0
我被OAuth令牌授权卡住了。我已配置OAuth,并拥有自己的OAuth服务器提供程序。通过授权属性验证OAuth不记名令牌
配置代码:
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AuthorizeEndpointPath = new PathString("/authorize"),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(1),
Provider = new SimpleAuthorizationServerProvider()
};
app.UseOAuthAuthorizationServer(OAuthServerOptions);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
服务器提供商:
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);
}
}
当我送:grand_type=password, username=MyUserName, password=MyPassword
到OAuth令牌端点"localhost/token"
,它很好地创造我的OAuth承载令牌。但是从这里开始,我不知道如何使用这个生成的令牌,它存储在哪里(如何获得它)以及如何使用ASP.NET MVC控制器上的[Authorize]
属性进行成功验证。我只是简单地想要使用我生成的令牌,当我从一个视图转到另一个视图时,它具有[Authorize]
属性,并成功通过它。我怎样才能做到这一点?
您可以创建生成的刷新令牌的一个cookie,这个可以跨多个视图访问,请记住您的访问令牌的有效期为1小时,您需要每小时生成一个新的访问令牌 – SoftwareNerd