【问题标题】:Unable to generate token with refresh_token无法使用 refresh_token 生成令牌
【发布时间】:2017-04-21 11:16:54
【问题描述】:

我正在向我的本地主机服务器传递一个刷新令牌,但获得了不受支持的授权类型响应(请参见图片)。

我正在使用以下代码使用刷新令牌生成令牌。

public class MyAuthorizationServerProvider: OAuthAuthorizationServerProvider
{
    public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
    {
        context.Validated(); //
    }
    public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        var identity = new ClaimsIdentity(context.Options.AuthenticationType);

        if (context.UserName == “admin” && context.Password == “admin”)
        {
           identity.AddClaim(new Claim(ClaimTypes.Role, “admin”));
           identity.AddClaim(new Claim(“username”, “admin”));
           identity.AddClaim(new Claim(ClaimTypes.Name, “admin”));
           context.Validated(identity);
        }
        else if (context.UserName == “dmuser” && context.Password == “dmuser”)
        {
           identity.AddClaim(new Claim(ClaimTypes.Role, “dmuser”));
           identity.AddClaim(new Claim(“username”, “dmuser”));
           identity.AddClaim(new Claim(ClaimTypes.Name, “dmuser”));
           context.Validated(identity);
        }
        else
        {
           context.SetError(“invalid_grant”, “Provided username and password is incorrect”);
           return;
        }
    }

    public override async Task GrantRefreshToken( OAuthGrantRefreshTokenContext context)
    {
     // chance to change authentication ticket for refresh token requests
        var newId = new ClaimsIdentity(context.Ticket.Identity);
        var newTicket = new AuthenticationTicket(newId, context.Ticket.Properties);
        context.Validated(newTicket);
     }
 }

如果有人知道为什么我的 GrantRefreshToken() 方法在使用 refresh_token 生成新令牌时没有调用。 请回复! 提前致谢!!!

【问题讨论】:

    标签: asp.net oauth-2.0


    【解决方案1】:

    尝试使用如下所示的标题。参考RFC6749

    POST /token HTTP/1.1
         Host: server.example.com
         Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
         Content-Type: application/x-www-form-urlencoded
    
         grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA
    

    【讨论】:

    • 感谢您的回复,但仍然无法正常工作。 GrantRefreshToken() 方法未调用。你能告诉我如何调试吗?
    • 它是否在处理您的邮递员请求?如果是这样,我们可以深入研究代码。你也可以试试 Curl 来测试你的请求。
    • 它不适用于邮递员请求。(请检查附图)当我发出邮递员请求时,方法 ValidateClientAuthentication 被调用,但下一个方法,即 GrantRefreshToken 没有被调用。
    猜你喜欢
    • 2022-01-14
    • 2021-03-14
    • 1970-01-01
    • 2016-11-01
    • 1970-01-01
    • 2020-08-11
    • 1970-01-01
    • 2015-02-21
    • 2020-06-23
    相关资源
    最近更新 更多