2016-09-15 59 views
2

我可以看到生成使用下面的代码 令牌的方式在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服务器有效的令牌。

其次,是否有可能有多个授权提供者可以有条件地使用此服务器。如果是这样的话,没有从零开始实现令牌生成器(如外部登录的东西),如何做到这一点?

回答

0

的OWIN引导http://www.asp.net/aspnet/overview/owin-and-katana/owin-oauth-20-authorization-server 有以下方法来生成令牌.....

private readonly ConcurrentDictionary<string, string> _authenticationCodes = 
    new ConcurrentDictionary<string, string>(StringComparer.Ordinal); 

private void CreateAuthenticationCode(AuthenticationTokenCreateContext context) 
{ 
    context.SetToken(Guid.NewGuid().ToString("n") + Guid.NewGuid().ToString("n")); 
    _authenticationCodes[context.Token] = context.SerializeTicket(); 
} 

private void ReceiveAuthenticationCode(AuthenticationTokenReceiveContext context) 
{ 
    string value; 
    if (_authenticationCodes.TryRemove(context.Token, out value)) 
    { 
     context.DeserializeTicket(value); 
    } 
} 

这是一种方式,但我仍然不知道这是MS实现它的官方途径。很高兴知道是否有内置函数可以做到这一点。

+1

注意:GUID是**不是**加密安全 – zerkms

+0

谢谢你,尽管它在MS官方指南上使用它,但我还在犹豫。你知道更好的方法吗? – heyNow

+0

请注意,此代码不会在“SetToken”上生成访问令牌。它正在生成一个用于OAuth授权代码授权类型的代码。 https://tools.ietf.org/html/rfc6749#section-4.1 – Daniel

0

您可以see the code here。这可能很难遵循。有很多是进入使其遵循OAuth规范,并可插拔(你可以换出例如令牌格式),但最终的默认配置是做这样的事情在幕后生成令牌:

var ticket = new AuthenticationTicket(new ClaimsIdentity(new GenericIdentity("bob")), new AuthenticationProperties()); 
    IDataProtector dataProtecter = null; 
    var format = new TicketDataFormat(dataProtecter); 
    var accessToken = format.Protect(ticket); 

通常除非您了解安全含义并确保现成的代码不足,否则不应对其进行过多定制或生成带外令牌。如果内置的东西不能满足您的需求,请考虑使用Identity Server。

相关问题