2017-09-06 1 views
1

ID와 EF를 사용하여 쿠키 인증을 구성하려고합니다. 지금까지 내 컨트롤러 응답에 유효한 Set-Cookie를 가질 수있었습니다. 브라우저는이 쿠키를 다시 전송하지만 AuthorizeFilter는 항상 로그인 페이지로 리디렉션되므로 인증이 작동하지 않는 것 같습니다. 구성을 지원해야합니까? 여기까지 시작에 내 ConfigureServices있어 :ASP.NET Core 2.0의 AuthorizationAttribute

public void ConfigureServices(IServiceCollection services) 
    { 
     services.AddMvc(); 

     services.AddCors(o => o.AddPolicy("Cors", builder => 
     { 
      builder.WithOrigins(Configuration["AllowedOrigins"].Split(",")) 
       .AllowAnyMethod() 
       .AllowCredentials() 
       .AllowAnyHeader(); 
     })); 

     services.AddDbContext<MyIdentityDbContext>(options => 
       options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); 

     services.AddIdentity<IdentityUser, IdentityRole>() 
       .AddEntityFrameworkStores<MyIdentityDbContext>() 
       .AddDefaultTokenProviders(); 

     services.ConfigureApplicationCookie(options => { 
      if (!String.IsNullOrEmpty(Configuration["AuthCookieDomain"])) 
      { 
       options.Cookie.Domain = Configuration["AuthCookieDomain"]; 
      } 
      options.Cookie.Name = Configuration["AuthCookieName"]; 
      options.Cookie.HttpOnly = false; 
      options.Cookie.SameSite = SameSiteMode.None; 
     }); 
    } 

을 그리고, 내 구성 시작에 :

public void Configure(IApplicationBuilder app, IHostingEnvironment env, 
     IServiceProvider serviceProvider) 
    { 
     if (env.IsDevelopment()) 
     { 
      app.UseDeveloperExceptionPage(); 
     } 

     app.UseCors("Cors"); 

     app.UseMvc(); 

     app.UseAuthentication(); 
    } 

는 그런 내 행동은 실제로 성공적으로

// GET api/values 
    [HttpPost] 
    public async Task<ActionResult> Post([FromBody] AuthPost post) 
    { 
     if (post == null || String.IsNullOrEmpty(post.UserName) || String.IsNullOrEmpty(post.Password)) 
     { 
      return BadRequest(); 
     } 

     var result = await signInManager.PasswordSignInAsync(post.UserName, post.Password, true, false); 
     if (result.Succeeded) 
     { 
      return Ok(); 
     } 
     return Unauthorized(); 
    } 

그리고 마지막으로 쿠키를 설정하는, 동의하지 않는 다른 동작 (항상 로그인으로 리디렉션)

[HttpGet] 
    [Authorize] 
    public async Task<ActionResult> Get() 
    { 
     var user = await userManager.GetUserAsync(User); 
     return Ok(new { UserName = user.UserName }); 
    } 

답변

3

OK, ConfigureApplicationCookie가 작동하는 방법입니다. 무슨 문제가 app.UseMvc() 전에 호출해야 잘못된 app.UseMvc();의 순서와 app.UseAuthentication();

app.UseAuthentication()이었다 발생!

관련 문제