如何在ASP.Net Core OpenIddict中的API令牌响应中包括.issues和.expires?

问题描述

我是实现令牌和开发.Net Core API的新手。我遵循了本教程:https://kevinchalet.com/2017/01/30/implementing-simple-token-authentication-in-aspnet-core-with-openiddict/

但是令牌端点返回此:

{
"token_type": "Bearer","access_token": "<access token>","expires_in": 60
}

我需要答复还显示如下的.issues和.expires。我该如何实现?这可能吗?

{
"access_token": "<access token>","token_type": "bearer","expires_in": 1799,"userName": <username>,".issued": "Fri,14 Aug 2020 06:54:49 GMT",".expires": "Fri,14 Aug 2020 07:24:49 GMT"
}

我从教程中获得以下代码

[ApiController]
public class AuthorizationController : ControllerBase
{
    [HttpPost("~/token"),Produces("application/json")]
    public IActionResult Exchange(OpenIdConnectRequest request)
    {
        if (request.IsPasswordGrantType())
        {
            // Validate the user credentials.
            // Note: to mitigate brute force attacks,you SHOULD strongly consider
            // applying a key derivation function like PBKDF2 to slow down
            // the password validation process. You SHOULD also consider
            // using a time-constant comparer to prevent timing attacks.
            if (request.Username != "alice@wonderland.com" ||
                request.Password != "P@ssw0rd")
            {
                return Forbid(OpenIddictServerDefaults.AuthenticationScheme);
            }
            // Create a new ClaimsIdentity holding the user identity.
            var identity = new ClaimsIdentity(
                OpenIddictServerDefaults.AuthenticationScheme,OpenIdConnectConstants.Claims.Name,OpenIdConnectConstants.Claims.Role);
            // Add a "sub" claim containing the user identifier,and attach
            // the "access_token" destination to allow OpenIddict to store it
            // in the access token,so it can be retrieved from your controllers.
            identity.AddClaim(OpenIdConnectConstants.Claims.Subject,"71346D62-9BA5-4B6D-9ECA-755574D628D8",OpenIdConnectConstants.Destinations.Accesstoken);
            identity.AddClaim(OpenIdConnectConstants.Claims.Name,request.Username,OpenIdConnectConstants.Destinations.Accesstoken);
            // ... add other claims,if necessary.
            var principal = new ClaimsPrincipal(identity);
            // Ask OpenIddict to generate a new token and return an OAuth2 token response.
            return SignIn(principal,OpenIddictServerDefaults.AuthenticationScheme);
        }
        throw new InvalidOperationException("The specified grant type is not supported.");
    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)