1
I have an API to log (Aspnet Identity) into the WEB MVC application.
- Access to WEB MVC application(Login Screen).
- I call the API by passing email and password, which was informed in the WEB MVC application.
- API returns with Token(Bearer).
Doubt is as follows, how do I authorize access to the WEB application classes with the [Authorize] clause, considering the Token that return the API?
--- Updating with the Code --
-- Method That Calls My API
private async Task<List<LoginModel>> GetUserNameByemailRestSharp(string email)
{
List<LoginModel> lst = new List<LoginModel>();
object param = new { Email = email };
try
{
IRestResponse restResponse = await APIIdentity.ResponseAPIPost_RestSharp("/api/Account/GetUserNameByemail", param);
var token = JToken.Parse(restResponse.Content);
var obj = JObject.Parse((string)token.ToString());
lst.Add(new LoginModel { UserName = obj["UserName"].ToString(), Email = obj["Email"].ToString(), Password = obj["Password"].ToString()});
}
catch (Exception ex)
{
lst.Add(new LoginModel { UserName = "", Email = "", Password = "" });
}
return lst;
}
-- Controller that the login screen of the WEB application MVC(Via Ajax) calls passing, email and password
[HttpPost]
public async Task<JsonResult> getLogin(string email, string password)
{
List<LoginModel> lstUser = null;
object result = null;
try
{
lstUser = await GetUserNameByemailRestSharp(email);
if (!string.IsNullOrEmpty(lstUser[0].UserName))
{
var lstToken = await GetToken(lstUser[0].UserName, password);
if (lstToken[0].ToString() != "200")
{
result = new { StatusCode = lstToken[0].ToString(), Decription = lstToken[1].ToString() };
}
else
{
//Token que retornado da API
result = new { StatusCode = lstToken[0].ToString(), Decription = "" };
}
}
else
{
result = new { StatusCode = "400", Decription = "E-mail não consta na base de dados!" };
}
}
catch (Exception ex)
{
result = new { StatusCode = "400", Decription = "E-mail não consta na base de dados!" };
}
return Json(result, JsonRequestBehavior.AllowGet);
}
The Object Result contains the Bearer Token, how do I authenticate the attribute [Authorize] of my MVC WEB application that is outside the API project considering that Token?
The question is not very clear. Which Token are you talking about? It’s a JWT, SAML, etc. Where’s the code? Please see this link to help you improve your question: https://answall.com/help/how-to-ask
– Renan