【问题标题】:How to do long lived tokens with Katana OAuth Bearer Tokens如何使用 Katana OAuth Bearer Tokens 做长寿命令牌
【发布时间】:2014-02-10 19:36:49
【问题描述】:

通过 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)
    };

我有一个托管在单独域上的单页应用程序,它将使用来自 Token 端点的不记名令牌与 webapi 交互。

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

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

这些令牌是短暂的。我现在想扩展我的应用程序,这样我就可以获得一个 repress 令牌或我不必一直进行身份验证的东西。 我的下一步是什么?

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);

    }
}

【问题讨论】:

  • 上线AuthenticationProperties properties = CreatePropertiesAsync(user);你的 CreatePropertiesAsync 方法是什么样的?
  • 前段时间,不知道代码停在哪里了。但我的工作围绕着在这里发布的东西:blogs.msdn.com/b/webdev/archive/2013/09/20/…,它也使用它。

标签: oauth owin katana


【解决方案1】:

我只需为其设置提供程序即可生成刷新令牌。

任何关于何时设置刷新令牌的指针的 cmets 都会很好。

 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);
    }

【讨论】:

  • 基本上 RefreshToken 帮助应用程序获取新的 AccessToken,即使在当前的 AccessToken 过期后也是如此。在获取新的 AccessToken 时,可以显示 RefreshToken,不需要用户名密码。 RefreshToken 的生命周期可以由 Token Provider 管理,并且应该为用户提供授予 RefreshToken 生命周期的选项。
  • 有道理,谢谢。只需要弄清楚如何在 Katana AuthorizationServer (Owin) 中做到这一点。似乎刷新令牌的生命周期与访问令牌相同。
  • 是的,刷新令牌是从与 accessToken 票证相同的票证创建的。如上所述,我们可以通过启动时提供的 ServerOptions 来更改它。
  • 但这意味着我们同时更改了刷新和访问令牌。我不想要像访问令牌这样有 20 分钟并刷新令牌直到被撤销的东西吗?
  • 对于accessToken,可以通过AccessTokenExpireTimeSpan。是的,您可以将刷新令牌保留更长的时间。
猜你喜欢
  • 2015-09-21
  • 1970-01-01
  • 1970-01-01
  • 2017-06-04
  • 1970-01-01
  • 1970-01-01
  • 2016-10-10
  • 2013-10-14
  • 2014-11-24
相关资源
最近更新 更多