【问题标题】:ASP.NET 5 OpenIdConnect Refresh_TokenASP.NET 5 OpenIdConnect Refresh_Token
【发布时间】:2016-06-14 06:14:38
【问题描述】:

我正在尝试使用refresh_token 创建一个token

关注this@Shaun Luttin 的回复

public sealed class AuthorizationProvider : OpenIdConnectServerProvider
{
    public override Task ValidateClientAuthentication(
        ValidateClientAuthenticationContext context)
    {
        // Since there's only one application and since it's a public client
        // (i.e a client that cannot keep its credentials private), call Skipped()
        // to inform the server the request should be accepted without 
        // enforcing client authentication.
        context.Skipped();

        return Task.FromResult(0);
    }

    public override Task GrantResourceOwnerCredentials(
        GrantResourceOwnerCredentialsContext context)
    {
        // Validate the credentials here (e.g using ASP.NET Identity).
        // You can call Rejected() with an error code/description to reject
        // the request and return a message to the caller.

        var identity =
            new ClaimsIdentity(OpenIdConnectServerDefaults.AuthenticationScheme);
        identity.AddClaim(ClaimTypes.NameIdentifier, "todo");

        // By default, claims are not serialized in the access and identity tokens.
        // Use the overload taking a "destination" to make sure your claims
        // are correctly inserted in the appropriate tokens.
        identity.AddClaim("urn:customclaim", "value", "token id_token");

        var ticket = new AuthenticationTicket(
            new ClaimsPrincipal(identity),
            new AuthenticationProperties(),
            context.Options.AuthenticationScheme);

        // Call SetResources with the list of resource servers
        // the access token should be issued for.
        ticket.SetResources(new[] { "resource_server_1" });

        // Call SetScopes with the list of scopes you want to grant
        // (specify offline_access to issue a refresh token).
        ticket.SetScopes(new[] { "profile", "offline_access" });

        context.Validated(ticket);

        return Task.FromResult<object>(null);
    }
}

当我请求这样的令牌时

POST http://localhost:50000/connect/token HTTP/1.1
User-Agent: Fiddler
Host: localhost:50000
Content-Length: 61
Content-Type: application/x-www-form-urlencoded

grant_type = password & username = my_username & password = my_password

我得到这样的令牌

{
  "resource": "resource_server_1",
  "scope": "profile offline_access",
  "token_type": "bearer",
  "access_token": "eyJh...W2rA",
  "expires_in": "3600"
}

它工作正常,但没有 refresh_token 属性初始化。我怎样才能得到它?

【问题讨论】:

    标签: authentication asp.net-core openid-connect aspnet-contrib


    【解决方案1】:

    由于某种原因我不知道指定

    ticket.SetScopes(new[] { "profile", "offline_access" });
    

    手动不起作用,所以我删除了这一行并将scope参数添加到我的请求标头中,现在我得到了refresh_token的响应

    POST http://localhost:50000/connect/token HTTP/1.1
    User-Agent: Fiddler
    Host: localhost:50000
    Content-Length: 61
    Content-Type: application/x-www-form-urlencoded
    
    grant_type = password & username = my_username & password = my_password & scope = offline_access
    

    所以现在的反应是

    {
      "resource": "resource_server_1",
      "scope": "profile offline_access",
      "token_type": "bearer",
      "access_token": "eyJh...W2rA",
      "refresh_token": "CfDJ8OV0Bu....AoUWPE"
      "expires_in": "3600"
    }
    

    【讨论】:

    • 是的,这是因为在 beta4 中,如果令牌请求中不存在 offline_access 范围(即使您明确授予它),它也会被忽略。这是我在 beta5 夜间构建中放松的地方。
    猜你喜欢
    • 2021-10-17
    • 1970-01-01
    • 2021-04-14
    • 2016-10-09
    • 2022-01-14
    • 1970-01-01
    • 2018-02-23
    • 2019-10-21
    • 2023-03-20
    相关资源
    最近更新 更多