如何限制用户在 ASP.NET Identity 中只能使用一个访问令牌
How to limit user to only one access token in ASP.NET Identity
我在我的 webApi 应用程序中使用基于令牌的身份验证。对于每次登录,OAuth 都会为用户生成一个访问令牌。如果用户尝试多次登录。它可能拥有一些更有效的令牌。
这个过程有限制吗?
这是我的 Startup class:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
//Rest of code is here;
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
这里是"GrantResourceOwnerCredentials"方法:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
恐怕令牌在过期之前一直有效,它将包含与用户相关的所有信息。
因此,要执行您想要的操作,您必须创建自己的层来验证用户是否拥有令牌,例如创建一个映射 table,然后创建一个自定义过滤器来拒绝用户的请求没有使用为他生成的最后一个令牌。
oauth 令牌的主要限制之一是它会过期。因此,如果您生成长寿令牌,那么它会长期有效。因此,处理此类 senerio 的一些常见方法是:
发行带有额外刷新令牌的短期令牌
将令牌存储在数据库中,每次生成新令牌时,都会使旧令牌状态过期。然后你可以编写自定义授权属性来检查令牌是否过期。
我在我的 webApi 应用程序中使用基于令牌的身份验证。对于每次登录,OAuth 都会为用户生成一个访问令牌。如果用户尝试多次登录。它可能拥有一些更有效的令牌。 这个过程有限制吗?
这是我的 Startup class:
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
ConfigureOAuth(app);
WebApiConfig.Register(config);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
app.UseWebApi(config);
//Rest of code is here;
}
public void ConfigureOAuth(IAppBuilder app)
{
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
}
这里是"GrantResourceOwnerCredentials"方法:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
恐怕令牌在过期之前一直有效,它将包含与用户相关的所有信息。
因此,要执行您想要的操作,您必须创建自己的层来验证用户是否拥有令牌,例如创建一个映射 table,然后创建一个自定义过滤器来拒绝用户的请求没有使用为他生成的最后一个令牌。
oauth 令牌的主要限制之一是它会过期。因此,如果您生成长寿令牌,那么它会长期有效。因此,处理此类 senerio 的一些常见方法是:
发行带有额外刷新令牌的短期令牌
将令牌存储在数据库中,每次生成新令牌时,都会使旧令牌状态过期。然后你可以编写自定义授权属性来检查令牌是否过期。