---
title: "How do you implement authentication and authorization when consuming .NET Core APIs in ASP.NET MVC?"  
description: "How do you implement authentication and authorization when consuming .NET Core APIs in ASP.NET MVC?"  
author: "Rocky Dada"  
published: 2023-08-30  
updated: 2023-08-30  
canonical: https://www.mindstick.com/forum/159785/how-do-you-implement-authentication-and-authorization-when-consuming-dot-net-core-apis-in-asp-dot-net-mvc  
category: "web api"  
tags: ["c#", ".net core", ".net core api"]  
reading_time: 2 minutes  

---

# How do you implement authentication and authorization when consuming .NET Core APIs in ASP.NET MVC?

Describe how to [implement authentication and authorization](https://www.mindstick.com/forum/159697/how-to-implement-authentication-and-authorization-in-a-dot-net-core-api) when consuming .NET [Core APIs](https://www.mindstick.com/forum/160208/how-entity-framework-core-simplify-database-access-in-dot-net-core-apis) in an ASP.NET [MVC application](https://www.mindstick.com/forum/12932/how-to-consume-webservices-from-asp-dot-net-mvc-application). Discuss passing tokens, handling [secure communication](https://answers.mindstick.com/qa/112358/can-you-explain-the-principles-of-quantum-cryptography-and-its-potential-for-secure-communication), and ensuring only [authorized](https://answers.mindstick.com/qa/51614/where-is-the-iphone-available-for-purchase-can-it-be-purchased-from-apple-authorized-resellers) users access the API.

## Replies

### Reply by Aryan Kumar

There are a few ways to implement [authentication and authorization](https://www.mindstick.com/forum/365/authentication-and-authorization-in-asp-dot-net-mvc) when consuming .NET Core APIs in [ASP.NET MVC](https://www.mindstick.com/forum/155798/what-is-caching-in-asp-dot-net-mvc). Here are two of the most common methods:

1. **Using the** `AuthorizeAttribute`**:** The `AuthorizeAttribute` is an attribute that can be applied to controller actions or methods to restrict access to them. The `AuthorizeAttribute` takes a single parameter, which is the role or roles that are allowed to access the action or method.
2. **Using the** `IdentityModel`**:** The `IdentityModel` is a class that provides a way to authenticate and authorize users in ASP.NET MVC applications. You can use the `IdentityModel` to authenticate users using a variety of methods, such as username and password, social login, or single sign-on.

Here is an example of how to use the `AuthorizeAttribute` to [implement authentication](https://www.mindstick.com/forum/160019/how-can-you-implement-authentication-in-a-node-js-application) and authorization when consuming a .NET Core API in ASP.NET MVC:

C#

```plaintext
[Authorize(Roles = "Admin")]
public class HomeController
{
    public async Task<IActionResult> Index()
    {
        // Only admins can access this action.
        return View();
    }
}
```

Here is an example of how to use the `IdentityModel` to implement authentication and authorization when consuming a .NET Core API in ASP.NET MVC:

C#

```plaintext
using Microsoft.AspNetCore.Identity;

public class HomeController
{
    private readonly UserManager<IdentityUser> _userManager;

    public HomeController(UserManager<IdentityUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task<IActionResult> Index()
    {
        // Only authenticated users can access this action.
        var user = await _userManager.GetUserAsync(User);
        if (user == null)
        {
            return RedirectToLogin();
        }

        return View();
    }
}
```

The best way to implement authentication and authorization when consuming .NET Core APIs in ASP.NET MVC will depend on the specific needs of your application. You should consider the factors such as the complexity of the API, the security requirements of your application, and the familiarity of your developers with the different techniques.


---

Original Source: https://www.mindstick.com/forum/159785/how-do-you-implement-authentication-and-authorization-when-consuming-dot-net-core-apis-in-asp-dot-net-mvc

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
