【问题标题】:How ASP Web API generate new token using refresh token during authorization of Http RequestASP Web API 在 Http 请求授权期间如何使用刷新令牌生成新令牌
【发布时间】:2018-02-06 21:40:11
【问题描述】:

我在我的 asp.net web api 中使用访问令牌。我使用 Angular4 作为客户端应用程序。登录后我得到了访问令牌和刷新令牌。我有一个授权属性来检查我的获取/发布请求是否有效。我在每个请求中发送带有访问令牌的刷新令牌。当我的访问令牌过期时,我的授权属性会阻止我访问 get/post 功能。如何使用有效的刷新令牌授权我的 get/post 函数并在验证 get/post 方法期间生成新的访问令牌。

【问题讨论】:

  • 我的回答对你有帮助吗?
  • 它对我有帮助。但是你能告诉我使用刷新令牌生成访问令牌的过程吗?

标签: angular asp.net-web-api oauth-2.0 owin access-token


【解决方案1】:

访问令牌和刷新令牌是两个不同的东西。

访问令牌用于访问资源。您在每次请求时都将访问令牌发送到资源。

刷新令牌用于获取新的访问令牌,而无需发送凭据。 刷新令牌被发送到授权端点。但仅在访问令牌过期后(返回未经授权的响应)。

确保在访问令牌过期之前刷新令牌不会过期。因为否则您将不得不再次发送凭据。

另请注意,刷新令牌应保密,因为它可用于检索令牌而无需发送凭据。始终通过安全线路发送。

-- 更新--

刷新令牌的服务器端

我假设您有一个 OAuthAuthorizationServerProvider 来处理登录。比如:

internal class ApplicationOAuthProvider : OAuthAuthorizationServerProvider
{
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {

为了选择加入,您可以覆盖 GrantRefreshToken 以接受刷新令牌:

public override async Task GrantRefreshToken(OAuthGrantRefreshTokenContext context)
{
    // chance to change authentication ticket for refresh token requests
    var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
    var appUser = await userManager.FindByNameAsync(context.Ticket.Identity.Name);
    var oAuthIdentity = await appUser.GenerateUserIdentityAsync(userManager);
    var newTicket = new AuthenticationTicket(oAuthIdentity, context.Ticket.Properties);

    context.Validated(newTicket);
}

添加提供程序以将刷新令牌添加到票证:

internal class ApplicationOAuthRefreshTokenProvider : AuthenticationTokenProvider
{
    public override void Create(AuthenticationTokenCreateContext context)
    {
        var form = context.Request.ReadFormAsync().Result;
        var grantType = form.GetValues("grant_type");

        // If I remember correctly we arrive here for all implemented grant types.
        // But we don't want to add a refresh token to the refresh token itself.

        if (grantType[0] != "refresh_token")
        {
            // 35 days.
            int expire = 35 * 24 * 60 * 60;
            context.Ticket.Properties.ExpiresUtc = new DateTimeOffset(DateTime.Now.AddSeconds(expire));
            context.SetToken(context.SerializeTicket());
        }
        base.Create(context);
    }

    public override void Receive(AuthenticationTokenReceiveContext context)
    {
        context.DeserializeTicket(context.Token);
        base.Receive(context);
    }

}

别忘了在启动时注册:

public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        // Other statements ...

        // Configure the application for OAuth based flow
        var oAuthOptions = new OAuthAuthorizationServerOptions
        {
            TokenEndpointPath = new PathString("/Token"),
            Provider = new ApplicationOAuthProvider("self"),
            AccessTokenExpireTimeSpan = TimeSpan.FromDays(1),
#if LIVE
            AllowInsecureHttp = false,
#else
            AllowInsecureHttp = true,
#endif
            RefreshTokenProvider = new ApplicationOAuthRefreshTokenProvider()
        };
        app.UseOAuthBearerTokens(oAuthOptions);
    }

【讨论】:

  • 你能告诉我如何在我的asp.net web api中使用刷新令牌来重新生成访问令牌。
猜你喜欢
  • 2019-01-30
  • 2018-04-11
  • 2016-12-22
  • 2016-10-13
  • 2018-08-21
  • 1970-01-01
  • 2019-08-05
  • 1970-01-01
  • 2016-01-05
相关资源
最近更新 更多