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();
}
我打電話註銷,然後用舊的令牌,但它仍然可以使用。所以註銷不起作用? 感謝收看。
你能幫我解決這個問題:https://stackoverflow.com/questions/47096113/token-based-implementation-in-webapi-to-secure-endpoints –