2016-07-22 1 views
0

owin을 사용하여 로그인했지만 로그 아웃 할 수 없습니다. 시작에서
다음 ApiController에서Owin을 사용하여 Web API를 로그 아웃 할 수 없음

 
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) 
     { 
      context.Validated(); 
      return Task.FromResult(null); 
     } 

     public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) 
     { 
      context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*"}); 
      using (demoEntities _repo = new demoEntities()) 
      { 
       if (!_repo.users.Where(x => x.username == context.UserName && x.pass == context.Password).Any()) 
       { 
        context.SetError("invalid_grant", "wrong."); 
        //context.Rejected(); 
        return; 
       } 
      } 
      //context.Request. 
      var identity = new ClaimsIdentity(context.Options.AuthenticationType); 
      identity.AddClaim(new Claim("sub", context.UserName)); 
      identity.AddClaim(new Claim("role", "user")); 
      identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName)); 
      if (context.Request.Path.Value != "/api/apidemo/logout") 
      { 
       context.Request.Context.Authentication.SignIn(identity); 
      } 
      else 
      { 
       context.Request.Context.Authentication.SignOut(); 
      } 

      context.Validated(identity); 
     } 


: 다음 AuthorizationServerProvider에서

 
public void ConfigureOAuth(IAppBuilder app) 
     { 
    OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions() 
      { 
       AllowInsecureHttp = true, 
       TokenEndpointPath = new PathString("/token"), 
       AccessTokenExpireTimeSpan = TimeSpan.FromMinutes(20), 
       Provider = new AuthorizationServerProvider(), 
       AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie    
      }; 
      app.UseOAuthBearerTokens(OAuthServerOptions); 
      app.UseCookieAuthentication(new CookieAuthenticationOptions()); 
     } 

[HttpGet] 
    [ActionName("logout")] 
    public IHttpActionResult logout() 
    { 
     Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
     this.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie); 
     return Ok(); 
    } 

그때 로그 아웃 전화 사용 된 토큰 만 그것은 여전히 ​​사용할 수 있습니다. 그래서 로그 아웃이 작동하지 않습니까? 감사합니다.

답변

3

그건 Owin의 작동 방식이 아닙니다. 로그 아웃이 없습니다. 당신은 토큰을 얻고 그 토큰은 정해진 시간 동안 유효합니다. 토큰은 만료 될 때까지 유효합니다.

기본적으로 토큰이 생성 될 때 추가 레이어를 추가 할 수 있습니다. 만료 데이터와 유효한 상태와 함께 어딘가에 저장하십시오. 로그 아웃을 호출 할 때 토큰을 유효하지 않은 것으로 업데이트 한 다음, 사용 된 경우, 오인 체크를 통과 한 후에 자신의 체크를 실행하고 무효화합니다.

솔직히 말해서 나는이 문제를 걱정하지 않을 것입니다. 이 경로를 따라 가다 보면 사용자 인증이 아닌 응용 프로그램 수준의 인증 인 Owin을 사용하지 않을 것임을 의미합니다. 둘 사이에 큰 차이가 있습니다.

그래서 내 충고는 회원 인증 시스템을 사용하여 사용자 인증을하고 소유권 항목을 별도로 유지하는 것입니다. 당신이 이것을 좋아한다면 실제로 누군가를 로그 아웃시킬 수 있습니다.

결론 : 오존 토큰은 만료 될 때까지 유효합니다.

+0

이 질문에 도움을 주시기 바랍니다 : https://stackoverflow.com/questions/47096113/token-based-implementation-in-webapi-to-secure-endpoints –

관련 문제