kin*_*kin 1 asp.net-web-api owin
我已经使用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());
}
在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();
}
Run Code Online (Sandbox Code Playgroud)
我调用logout然后使用旧令牌,但它仍然可以使用.注销无法正常工作?谢谢观看.
这不是Owin的工作方式.没有注销.您获得一个令牌,该令牌在设定的时间内有效.令牌有效期到期满为止.
您可以自己添加一个额外的层,基本上在生成令牌时,将其与其到期数据和有效状态一起存储在某处.当您调用logout时,您将令牌更新为无效,然后当它被使用时,在它通过owin检查后,您将运行自己的检查并使其无效.
说实话,我不打扰这个.如果你沿着这条路走下去,就意味着你没有使用Owin来做它的意图,即应用程序级认证,而不是用户认证.两者之间存在巨大差异.
因此,我的建议是使用会员系统进行用户身份验证,并将owin内容分开.如果您这样做,那么您实际上可以将某人记录下来.
所以底线:owin令牌在到期之前有效.
| 归档时间: |
|
| 查看次数: |
1656 次 |
| 最近记录: |