如何使用Katana OAuth Bearer Tokens做长寿代币

时间:2013-10-16 14:37:14

标签: oauth owin katana

在SPA模板中,我设法让基本的OAuth流程正常运行。

    OAuthOptions = new OAuthAuthorizationServerOptions
    {
        AllowInsecureHttp = true, 
        ApplicationCanDisplayErrors = true,
        TokenEndpointPath = new Microsoft.Owin.PathString("/Token"),
        AuthorizeEndpointPath = new Microsoft.Owin.PathString("/api/Account/ExternalLogin"),
        Provider = new CompositeWebroleOauthProvider<User>(PublicClientId, IdentityManagerFactory, CookieOptions)
    };

我有一个单页面应用程序托管在一个单独的域上,该域将使用来自令牌端点的承载令牌与webapi进行交互。

我正在使用包含以下数据的请求执行ResourceOwnerCredentials流程:

 data: {
        grant_type: "password",
        username: username,
        password: password
       }

这些代币是短暂的。我现在想扩展我的应用程序,这样我就可以获得一个refress令牌或类似的东西,我不需要一直进行身份验证。 我接下来的步骤是什么?

GrantResourceOwnerCredentials实施:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
    using (var identityManager = _identityManagerFactory.Create())
    {
        var user = await identityManager.FindAsync(context.UserName, context.Password);

        if (user == null)
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
            return;
        }               

        ClaimsIdentity oAuthIdentity = await identityManager.CreateIdentityAsync(user, context.Options.AuthenticationType);
        AuthenticationProperties properties = CreatePropertiesAsync(user);
        AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
        context.Validated(ticket);

    }
}

1 个答案:

答案 0 :(得分:0)

我只需要为它设置提供程序以生成刷新令牌。

关于何时设置刷新令牌的指针的任何评论都不错。

 RefreshTokenProvider = new AuthenticationTokenProvider
 {
     OnCreate = CreateRefreshToken,
     OnReceive = ReceiveRefreshToken,
 }


    private void CreateRefreshToken(AuthenticationTokenCreateContext context)
    {
        context.SetToken(context.SerializeTicket());
    }

    private void ReceiveRefreshToken(AuthenticationTokenReceiveContext context)
    {
        context.DeserializeTicket(context.Token);
    }