1
votes

I have an asp.net web api as authorization server where it generate bearer token with user claims when passing ussr/pass to its endpoint. Now i need to authenticate my asp.net mvc first by getting a token and then authorize my mvc action methods using that token ( not calling from javascript, imagine user clicks on about menu and the action method trigger) . I have no idea how and where I should decode and create an identity with those claims to authorize the method? Appreciate your help.

1

1 Answers

0
votes

here is the thing you can call HttpRequest to APIs from your controller side and then pass the result to your View

new when you click a menu bar or some link in MVC it will first hit the controller method AS You know all you need to do is link your controoler method to your api methods something like this example

  public async Task<ActionResult> YourMethod()
        {
            HttpClient client = new HttpClient();
            var content = new StringContent(Convert.ToString(JsonConvert.SerializeObject(anyobjectToPassifYouHaveOne)), Encoding.UTF8, "application/json");
            HttpResponseMessage result = await client.PostAsync(_API URL, content);
            if (result.IsSuccessStatusCode)
            {
                return result.Content.ReadAsAsync<bool>().Result;
            }
            return await Task.FromResult<bool>(false);
        }

this is just a sample method on how to access your apis

note since you are using jwt Token you might want to save the token in a session and pass it with every api request add this in your header