1
I have a API that return token bearer, normal, as per image
The question is this, it has an application MVC that consumes this API, and I want to put [Authorize] in the classes of that MVC application and authenticating with the API, is possible? if yes, how should I do?
private async Task<List<string>> GetToken(string UserName, string password)
{
List<string> lstResult = new List<string>();
string[] resultResponse = new string[2];
var parametros = new Dictionary<string, string>();
LoginModel param = new LoginModel { UserName = UserName, Password = password};
IRestResponse restResponse = await APIIdentity.ResponseAPIPostToken_RestSharp("/token", param);
var token = JToken.Parse(restResponse.Content);
var obj = JObject.Parse((string)token.ToString());
int statusCode = (int)restResponse.StatusCode;
if ((int)statusCode > 200)
{
resultResponse[0] = Convert.ToString((int)statusCode);
resultResponse[1] = obj["error_description"].ToString();
}
else
{
resultResponse[0] = Convert.ToString((int)statusCode);
resultResponse[1] = obj["access_token"].ToString();
}
lstResult.AddRange(resultResponse);
return lstResult;
}
public static async Task<IRestResponse> ResponseAPIPostToken_RestSharp(string URIResource, LoginModel objParam)
{
RestClient restClient = new RestClient(string.Format("{0}", ConfigurationManager.AppSettings["APIUrl"]));
RestRequest restRequest = new RestRequest("/token", Method.POST);
string encodedBody = string.Format("grant_type=password&username={0}&password={1}", objParam.UserName, objParam.Password);
restRequest.AddParameter("application/x-www-form-urlencoded", encodedBody, ParameterType.RequestBody);
restRequest.AddParameter("Content-Type", "application/x-www-form-urlencoded", ParameterType.HttpHeader);
return await restClient.ExecuteTaskAsync(restRequest);
}