2016-07-22 101 views
0

我使用owin登录但无法注销。
在开始:
使用Owin无法注销Web Api

 
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()); 
     } 

在AuthorizationServerProvider:

 
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); 
     } 


在ApiController:

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

我打电话注销,然后用旧的令牌,但它仍然可以使用。所以注销不起作用? 感谢收看。

回答

3

这不是Owin的工作方式。没有注销。你得到一个令牌,该令牌在一段时间内有效。该令牌在到期前一直有效。

你可以自己添加一个额外的图层,基本上当一个令牌生成时,将它存储在某个地方以及它的到期数据和有效状态。当你打电话注销时,你将令牌更新为无效,那么当它被使用时,在通过owin检查后,你运行自己的支票并使其无效。

说实话,我不会为此而烦恼。如果你沿着这条路线走下去,这意味着你没有使用Owin来完成它的意图,这是应用程序级认证,而不是用户认证。两者之间有巨大的差异。

所以,我的建议是使用会员系统进行用户身份验证,并将owin的东西分开。如果你这样做,那么你实际上可以注销某人。

因此,底线:owin代币有效期到期。

+0

你能帮我解决这个问题:https://stackoverflow.com/questions/47096113/token-based-implementation-in-webapi-to-secure-endpoints –