C# OWIN究竟使用什么函数来生成令牌;它能被利用吗?

C# OWIN究竟使用什么函数来生成令牌;它能被利用吗?,c#,oauth,asp.net-web-api2,token,owin,C#,Oauth,Asp.net Web Api2,Token,Owin,我可以看到使用以下代码生成令牌的方法 如中所述 OAuthAuthorizationServerOptions OAuthServerOptions=new OAuthAuthorizationServerOptions() { AllowInsecureHttp=true, TokenEndpointPath=新路径字符串(“/token”), AccessTokenExpireTimeSpan=TimeSpan.FromDays(1), Provider=新的SimpleAuthoriza

我可以看到使用以下代码生成令牌的方法 如中所述

OAuthAuthorizationServerOptions OAuthServerOptions=new OAuthAuthorizationServerOptions()
{
AllowInsecureHttp=true,
TokenEndpointPath=新路径字符串(“/token”),
AccessTokenExpireTimeSpan=TimeSpan.FromDays(1),
Provider=新的SimpleAuthorizationServerProvider()
};
//令牌生成
使用OAuthAuthorizationServer(OAuthServerOptions);
var bearerAuth=新的OAuthBeareAuthenticationOptions()
{
Provider=新的OAuthBeareAuthenticationProvider()
};
应用程序UseAuthBeareAuthentication(BeareAuth);
公共类SimpleAuthorizationServerProvider:OAuthAuthorizationServerProvider
{
公共重写异步任务ValidateClientAuthentication(OAuthValidateClientAuthenticationContext)
{
context.Validated();
}
公共重写异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentials上下文)
{
context.OwinContext.Response.Headers.Add(“访问控制允许来源”,新[]{“*”});
var manager=newusermanager(newuserstore(newuserrepository());
var user=wait manager.FindAsync(context.UserName,context.Password);
if(user==null)
{
SetError(“无效的授权”,“用户名或密码不正确”);
}
其他的
{
var identity=newclaimsidentity(context.Options.AuthenticationType);
identity.AddClaim(新声明(“名称”,user.Email));
上下文验证(身份);
}
}
}
作为一个消费者,我用我的凭证打一个REST呼叫,然后
神奇地获得一个访问令牌

我希望能够利用该令牌生成功能在其他场景中使用,手动创建一个令牌,该令牌对OWIN服务器中的
**此特定**
有效

其次,是否可能有多个授权提供程序可由该服务器有条件地使用。如果是这样的话,在不从头实现令牌生成器(如外部登录)的情况下,如何做到这一点

OWIN指南 使用以下方法生成令牌

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);
     }
 }
专用只读ConcurrentDictionary\u authenticationCodes=
新的ConcurrentDictionary(StringComparer.Ordinal);
私有void CreateAuthenticationCode(AuthenticationTokenCreateContext上下文)
{
context.SetToken(Guid.NewGuid().ToString(“n”)+Guid.NewGuid().ToString(“n”));
_authenticationCodes[context.Token]=context.SerializeTicket();
}
私有void ReceiveAuthenticationCode(AuthenticationTokenReceiveContext上下文)
{
字符串值;
if(_authenticationCodes.TryRemove(context.Token,out值))
{
DeserializeTicket(值);
}
}
这是一种方式,但我仍然不知道这是否是微软实现它的官方方式。如果有一个内置函数可以实现这一点,那就太好了。

您可以。这可能很难理解。要使其遵循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);

通常,除非您了解安全含义,并且确信开箱即用的代码是不够的,否则您不应该对其进行过多的自定义或生成带外令牌。如果内置的东西不能做你需要的,考虑一下身份服务器。

它有什么价值,这里是我们如何做:

        var options = new OAuthAuthorizationServerOptions();
        var ticket = new AuthenticationTicket(...);

        var tokenContext = new AuthenticationTokenCreateContext(null, options.AccessTokenFormat, ticket);
        await context.options.AccessTokenProvider.CreateAsync(tokenContext);
        var token = tokenContext.Token;
        if (string.IsNullOrEmpty(token)) token = tokenContext.SerializeTicket();
        return token;
选项必须是应用程序中的OAuthAuthorizationServer选项。使用OAuthAuthorizationServer(选项)调用

这主要复制了OAuthAuthorizationServerHandler生成承载令牌的方式。

注意:GUID不是加密安全的谢谢,尽管它在MS官方指南上,我还是犹豫了一下。您知道更好的方法吗?请注意,此代码不是在“SetToken”上生成访问令牌。它正在生成用于OAuth授权代码授权类型的代码。
        var options = new OAuthAuthorizationServerOptions();
        var ticket = new AuthenticationTicket(...);

        var tokenContext = new AuthenticationTokenCreateContext(null, options.AccessTokenFormat, ticket);
        await context.options.AccessTokenProvider.CreateAsync(tokenContext);
        var token = tokenContext.Token;
        if (string.IsNullOrEmpty(token)) token = tokenContext.SerializeTicket();
        return token;