我已经使用 owin 登录,但无法退出。
在开始:
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()); }
In the 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); }
In the ApiController :
[HttpGet]
[ActionName("logout")]
public IHttpActionResult logout()
{
Request.GetOwinContext().Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
this.Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
return Ok();
}
我调用注销然后使用旧 token ,但它仍然可以使用。所以注销不起作用? 感谢观看。
最佳答案
这不是 Owin 的工作方式。没有注销。您将获得一个 token ,该 token 在一定时间内有效。 token 在到期前一直有效。
您可以自己添加一个额外的层,基本上是在生成 token 时,将其与过期数据和有效状态一起存储在某个地方。当您调用注销时,您将 token 更新为无效,然后当它被使用时,在它通过 owin 检查后,您然后运行您自己的检查并使其无效。
老实说,我不会为此烦恼。如果您沿着这条路线走下去,则意味着您没有使用 Owin 来实现它的目的,即应用程序级身份验证,而不是用户身份验证。两者之间存在巨大差异。
因此,我的建议是使用成员(member)系统进行用户身份验证,并将 owin 的内容分开。如果你这样做,那么你实际上可以将某人注销。
所以底线是:owin token 在过期之前一直有效。
关于asp.net-web-api - 无法使用 Owin 注销 Web Api,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38518457/