简体   繁体   English

向不记名令牌 json 添加更多值

[英]Add more values to the bearer token json

I want to return the user name after they logged in successfully to display on the upper right corner on my web app.我想在他们成功登录后返回用户名以显示在我的网络应用程序的右上角。 I want to send it with the json that bearer the token returns.我想将它与承载令牌返回的 json 一起发送。 To generate the token authentication I'm using ASP.NET web API and Owin middlehawe.为了生成令牌身份验证,我使用了 ASP.NET Web API 和 Owin middlehawe。

{   
  "access_token": "blah",
  "token_type": "bearer",
  "expires_in": 599
}

I want the return to be like this我希望回报是这样的

{   
  "access_token": "blah",
  "token_type": "bearer",
  "expires_in": 599,
  "displayusername":"Hi Mundo"
}

I have tried claims but those are not giving the result I want.我已经尝试过索赔,但那些并没有给出我想要的结果。

I have tried to use AuthenticationProperties but doesn't work我曾尝试使用 AuthenticationProperties 但不起作用

 public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
    {
        context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
        if (validation works)
        {
           // add claims

            var moreInfo = new AuthenticationProperties(new Dictionary<string, string> { { "username", "Don"}, { "Department","MIS"} });

            var ticket = new AuthenticationTicket(identity, moreInfo);
            context.Validated(ticket);
        }
        else
        {
            context.SetError("invalid_grant", "The user name or password is incorrect.");
        }
    }

How do I add more values to the json that the owin bearer token returns?.如何向 owin 不记名令牌返回的 json 添加更多值?。

Override TokenEndpointResponse on same class you are checking for validity and return additional field as shown in sample below在您检查有效性的同一类上覆盖TokenEndpointResponse并返回附加字段,如下面的示例所示

public override Task TokenEndpointResponse(OAuthTokenEndpointResponseContext context)
{
    string thisIsTheToken = context.AccessToken;
    //add user Id and status as additional response parameter
    context.AdditionalResponseParameters.Add("displayusername", "Hi Mundo");
    context.AdditionalResponseParameters.Add("Status", "1");     
    return base.TokenEndpointResponse(context);
}

you can simple do it by Add new item to moreInfo.Dictionary :您可以通过将新项目添加到moreInfo.Dictionary来简单地做到这moreInfo.Dictionary

moreInfo.Dictionary.Add("username", user.UserName);
moreInfo.Dictionary.Add("Department","MIS");

in ApplicationOAuthProvider.cs file add your properties in below method :在 ApplicationOAuthProvider.cs 文件中,在以下方法中添加您的属性:

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
            {
              
                AuthenticationProperties properties = CreateProperties(user.UserName,user.FirstName,user.LastName);
              
            }




public static AuthenticationProperties CreateProperties(string userName,string FirstName,string LastName)
        {
            IDictionary<string, string> data = new Dictionary<string, string>
            {
                { "userName", userName },
                { "FirstName", FirstName },
                { "LastName", LastName },
            };
            return new AuthenticationProperties(data);
        }

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

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