【发布时间】:2016-12-02 19:54:43
【问题描述】:
我已经按照本教程实现了ASP.NET Identity认证和OAuth授权:http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
它目前正在工作,但我不完全了解 TOKEN 及其计时器的存储位置。
这是生成令牌的代码:
public class SimpleAuthorizationServerProvider : OAuthAuthorizationServerProvider
{
public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
context.Validated();
}
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);
}
}
我猜这个令牌存储在 ASP.NET 身份数据库或托管的 WEB API 应用程序中,但我不完全理解。
【问题讨论】:
-
令牌在应用程序的内存中。默认情况下它不会存储在任何地方。您应该查看 IdentityServer 实现以获得更强大的 oauth 令牌提供程序。
标签: c# asp.net-web-api oauth