如何获取已认证用户的访问令牌以进行授权的资源api调用?

时间:2018-11-30 08:11:36

标签: c# asp.net-mvc security oauth-2.0 claims-based-identity

我有一个简单的MVC 5 Owin项目,该项目使用Auth服务器进行身份验证以请求令牌。用户登录后,如何获取用户令牌以在API上发出其他受保护的请求?是否将其存储为当前记录的ClaimsIdentity的声明?

2 个答案:

答案 0 :(得分:0)

 private static string _strSISApiUrl = "YourAPIURL";

  public static string GetUserAuthenticationToken(string strUserID, string strPwd)

  {
    #if(SISAVILABLE)

    List<KeyValuePair<string,string>> objKeyValuePair= new List<KeyValuePair<string,string>>();
                objKeyValuePair.Add(new KeyValuePair<string,string>("username",strUserID));
                objKeyValuePair.Add(new KeyValuePair<string,string>("password",strPwd));
                string jSONResult = RestHttpClient.HttpPost(_strSISApiUrl + "login", objKeyValuePair);
                return jSONResult;
            #else
                return "{auth_token:'your api token'}";
            #endif
        }

答案 1 :(得分:0)

public class Users
{
    [Required]
    [StringLength(6, MinimumLength = 3)]
   //[Display(Name = "User Name")]
    [RegularExpression(@"(\S)+", ErrorMessage = "White space is not allowed")]
    [ScaffoldColumn(false)]
    public string UserName { get; set; }

    [Required]
  //  [Display(Name = "Password")]
    private string _AuthenticatedToken ;
    public string Password { get; set; }
    public string UserID { get; set; }
    public string val { get; set; }
    public string Test { get; set; }
    public string AuthenticatedToken {
        get {
                return _AuthenticatedToken;
            }
    }
    public bool AuthenticateUser() 
    {
        SISInterfaceBus objSISInterfaceBus = new SISInterfaceBus();
        SISValidateUserResultSet objSISValidateUserResultSet = objSISInterfaceBus.ValidateLogin(this.UserName,this.Password);
        _AuthenticatedToken = objSISValidateUserResultSet.SISAuthToken;
        return objSISValidateUserResultSet.IsValidUser;
    }
}