2014-12-11 7 views
4

ASP.NET과 ID 2.0으로 웹 API를 작성했습니다. 사용자가 성공적으로 '로그인'한 경우에만 API에 액세스 할 수 있어야합니다. 로그인이 훌륭하게 작동하지만 로그 아웃 (로그 아웃)이 작동하지 않는 것 같습니다. 여기에 내가 사용하고 일부 코드는 다음과 같습니다ASP.NET ID 2.0 로그인이 작동하지 않습니다.

정체성 구성 :

public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; } 

public void Configuration(IAppBuilder app) 
{ 
    app.CreatePerOwinContext<IdentityDbContext<IdentityUser>>(HLAccountManager.CreateDbContext); 
    app.CreatePerOwinContext<UserManager<IdentityUser>>(HLAccountManager.CreateUserManager); 

    OAuthBearerOptions = new OAuthBearerAuthenticationOptions(); 

    app.UseOAuthBearerAuthentication(OAuthBearerOptions); 
    app.UseCookieAuthentication(new CookieAuthenticationOptions 
    { 
     AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
     LoginPath = new PathString("/Account/Login") 
    }); 

    GlobalConfiguration.Configuration.SuppressDefaultHostAuthentication(); 
    GlobalConfiguration.Configuration.Filters.Add(new HostAuthenticationFilter("Bearer")); 
} 

인증 컨트롤러 : 인증 용-방법은 잘 작동

[HttpPost] 
[ActionName("Authenticate")] 
[AllowAnonymous] 
public String Authenticate(JObject data) 
{ 
    dynamic json = data; 
    string user = json.user; 
    string password = json.password; 

    if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(password)) 
     return "failed"; 

    var userIdentity = UserManager.FindAsync(user, password).Result; 
    if (userIdentity != null) 
    { 
     var identity = new ClaimsIdentity(IdentityConfig.OAuthBearerOptions.AuthenticationType); 
     identity.AddClaim(new Claim(ClaimTypes.Name, user)); 
     identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userIdentity.Id)); 
     AuthenticationTicket ticket = new AuthenticationTicket(identity, new AuthenticationProperties()); 
     var currentUtc = new SystemClock().UtcNow; 
     ticket.Properties.IssuedUtc = currentUtc; 
     ticket.Properties.ExpiresUtc = currentUtc.Add(TimeSpan.FromMinutes(30)); 
     string AccessToken = IdentityConfig.OAuthBearerOptions.AccessTokenFormat.Protect(ticket); 
     return AccessToken; 
    } 
    return "failed"; 
} 

[HttpGet] 
[Authorize] 
[ActionName("Logout")] 
public String Logout() 
{ 
    var owinContext = HttpContext.Current.GetOwinContext(); 
    owinContext.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie, DefaultAuthenticationTypes.ExternalBearer); 

    return "OK"; 
} 

. 내 webapp는 요청에서 토큰을 가져와 인증 헤더 (예 : 각도 앱의 경우 $ http)로 설정할 수 있습니다. [인증] 주석 처리 된 함수에 대한 후속 호출은 올바르게 반환됩니다. 그러나 Logout을 호출하면 "OK"문자열이 올바르게 반환되지만 토큰은 무효화되지 않습니다. Logout을 호출 한 후 Authorize-method를 호출하면 401 - Unauthorized가 아닌 올바른 값을 얻을 수 있습니다.

  • 이 게시물 : ASP.Net Identity Logout을 보았습니다. 매개 변수없이 Signout을 시도했습니다. 그것도 작동하지 않습니다.
  • HttpContext에는 GetOwinContext가 없습니다. 제 경우에는 HttpContext.Current에 있습니다. 내가 뭔가 잘못하고 있는거야?
  • 내 로그 아웃 방법이 작동하지 않는 이유는 무엇입니까?

답변

5

내가 (무기명) 토큰의 기본 개념을 잘못 알아 낸 것 같아 그것이 작동하지 않는 이유입니다. 누군가가 같은 문제에 걸려 넘어 질 경우를 대비하여 여기에 남겨 두었습니다.

토큰을 철회하거나 무효화 할 수 없습니다. 적어도 ASP.NET Identity 2.0에서는 그렇지 않습니다. SignOut은 이러한 종류의 인증을 위해 을 사용할 수 없습니다.

이를위한 솔루션은 새로 고침 토큰입니다. 현재 Identity 2.0 또는 OWIN에는 기본 구현이 없습니다.

:하지만이 솔루션이 개 블로그 게시물을 발견
관련 문제