簡體   English   中英

在WebApi2中使用Owin返回帶有Bearer令牌的自定義類型

[英]Return custom type with Bearer Tokens using Owin in WebApi2

我需要在Web api項目中實現OWIN身份驗證。 我可以執行相同的操作,並且還可以返回多個索賠。 但是我想在成功登錄后返回UserDetails類型值。

public override async Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{

}
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
        var identity = new ClaimsIdentity(context.Options.AuthenticationType);
        identity.AddClaim(new Claim("UserName", userDetails.UserName));
        identity.AddClaim(new Claim("Iata", userDetails.Iata));
        context.Validated(identity);

        UserDetailsDTO userDetails = UserDetailService.GetUserdetailExternal(context.UserName, context.Password, userDetails.Iata);

       // I want to return userDetails where i need help

}
public override Task TokenEndpoint(OAuthTokenEndpointContext context)
{
    foreach (KeyValuePair<string, string> property in context.Properties.Dictionary)
    {
        context.AdditionalResponseParameters.Add(property.Key, property.Value);
    }
    return Task.FromResult<object>(null);
}

public class UserDetailsDTO
{
    public string UserName { get; set; }
    public string Iata { get; set; }
    public string LoginId { get; set; }
    public Screen Screens { get; set; }
    public string ErrorText { get; set; }
    public string Roles { get; set; }
    public bool IsADUser { get; set; }
}

public class Screen
{
    public string ScreenName { get; set; }
    public string ScreenUrl { get; set; }

}

JSON輸出

"access_token": "pG-Ho6S_0-JGdBcUwmtuGBpuoAECjC8T0bs93_FtmOw7dzUezgTRHrbyos67DM8xJmDrjF8vHVBXNlyRUoHxABFA2NOdMvtYPOaoKNMAES1jPN6gWcOQwZN5Tlcyc4qYkXCUGnVuVFncLZIvf0aBIZwUKiJDW4OxCJuh7wRoe49XUCh3hCd8-8R6ZcTy3VKGQlJmLau1pFaHZsqMeGdvbPvxSmixptGp29u76QThnovr8XMmh65f9o1JWDer6CsQSnd2VG1NEkrkhBWur2Jyz6EdEyW-ZbVeEvlw28P_i899RTEjhTBKBlo7m8sLJKViGDEO-uW0r9iBDThp2F-V-0ggp_wfsBHQDbTjqoddSITFAxwe7Vpm1BQAptyF_-Fu2oV-CbzVWucD8dr-PQKBlN8m3tFv4lMO2681X9bApF5yivkDvEMYW5_cNO5hzIOvsQ8hUh7oY5iF-mVZkUgXHmk9pqbAYQ9evTK3MCtDS-XLKqvdkb90BwDFYW6jKg_Sv7IcuYbHv7i5jTwSM8K6MySyjha_Y_4y1VVhx5QSe4afFnH_Z5nNimrGDJgi5mZbRbiKDFeR28qjxXZWmJ8ISyxQRZmT_JVZArGDpu-hVoUcz9CDLvHeCcL2a9uXmLm0",
  "token_type": "bearer",
  "expires_in": 86399,
  ".issued": "Thu, 22 Mar 2018 15:35:56 GMT",
  ".expires": "Fri, 23 Mar 2018 15:35:56 GMT",
  "UserName": "rmishra",
  "Iata": "AU"

我無法使用bearer_token在JSON對象中獲取userDetails。 有人可以幫我退還嗎?

您需要在身份驗證屬性中分配userdetails json字符串,然后生成身份驗證票證並驗證身份驗證票證。

UserDetailsDTO userDetails = UserDetailService.GetUserdetailExternal(context.UserName, context.Password, userDetails.Iata);

var identity = new ClaimsIdentity(context.Options.AuthenticationType);
identity.AddClaim(new Claim("UserName", userDetails.UserName));
identity.AddClaim(new Claim("Iata", userDetails.Iata));
IDictionary<string, string> authProp = new Dictionary<string, string>()
{
    { "UserName", userDetails.UserName },
    { "Iata", userDetails.Iata},
    { "UserDetails", Newtonsoft.Json.JsonConvert.SerializeObject(userDetails)}
};
var ticket = new AuthenticationTicket(identity, authProp);
context.Validated(ticket); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM