繁体   English   中英

Web api 2身份验证中无法使用令牌访问其他数据

[英]Unable to access additional data with token in Web api 2 authentication

我正在使用Web Api 2并实现了基于自定义令牌的身份验证。 它工作正常,但我想获得一些额外的属性值作为响应。 即使我添加了新的声明并添加了新的属性来获取它们的值作为响应,但我仍然仅获得三个值:“ access_token”,“ token_type”和“ expires_in”。 如何获得更多的价值作为回应。 这是我的代码:

  public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        if(context.UserName == "user" && context.Password=="user")
        {
             var identity = new ClaimsIdentity(context.Options.AuthenticationType);
            identity.AddClaim(new Claim(ClaimTypes.Name, context.UserName));
            identity.AddClaim(new Claim(ClaimTypes.Role, "Administrators"));

            identity.AddClaim(new Claim("MyClaim", "I don't know"));

            var props = new AuthenticationProperties(new Dictionary<string, string>
            {
                {  "name", "John" },
                { "surname", "Smith" },
                { "age", "40" },
                { "gender", "Male" }
            });

            var ticket = new AuthenticationTicket(identity, props);
            context.Validated(ticket);
        }
        else
        {
            context.SetError("Invalid_Grant", "Provided username and password is incorrect");
            return;
        }
    }

这是我得到的输出

{

“ access_token”:“ xxxxx”,“ token_type”:“ bearer”,“ expires_in”:86399}

附加声明和属性不应在响应中显示为附加字段,而是在access_token本身中进行编码。 如果您使用的是JWT(JSON Web令牌),则可以在https://jwt.io/上查看生成的令牌的内容。只需将令牌粘贴到左侧窗口中,然后在右侧看到包含所有声明的已解码令牌。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM