我可以看到生成使用下面的代码 令牌的方式在owin使用什么函数来生成令牌;它可以被利用吗?
http://bitoftech.net/2014/06/01/token-based-authentication-asp-net-web-api-2-owin-asp-net-identity/
OAuthAuthorizationServerOptions OAuthServerOptions = new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
Provider = new SimpleAuthorizationServerProvider()
};
// Token Generation
app.UseOAuthAuthorizationServer(OAuthServerOptions);
var bearerAuth = new OAuthBearerAuthenticationOptions()
{
Provider = new OAuthBearerAuthenticationProvider()
};
app.UseOAuthBearerAuthentication(bearerAuth);
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[] { "*" });
var manager = new UserManager<User, long>(new UserStore(new UserRepository()));
var user = await manager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
}
else
{
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("name",user.Email));
context.Validated(identity);
}
}
}
作为消费者我做一个REST调用http://localhost:9000/token与我的凭据和magically
概述获得令牌
我希望能够利用该令牌生成功能在其他场景中使用,以手动创建一个对OWL服务器有效的令牌。
其次,是否有可能有多个授权提供者可以有条件地使用此服务器。如果是这样的话,没有从零开始实现令牌生成器(如外部登录的东西),如何做到这一点?
注意:GUID是**不是**加密安全 – zerkms
谢谢你,尽管它在MS官方指南上使用它,但我还在犹豫。你知道更好的方法吗? – heyNow
请注意,此代码不会在“SetToken”上生成访问令牌。它正在生成一个用于OAuth授权代码授权类型的代码。 https://tools.ietf.org/html/rfc6749#section-4.1 – Daniel