Using Azure Active Directory authentication in ASP.NET Core 2.0 from Web App to Web API

DJ Grossman

I am trying to pass Azure Active Directory credentials from an ASP.NET Core 2.0 Web App to an ASP.NET Core 2.0 Web API so that the Web API can react based on the user's properties and permissions.

There are, admittedly, quite a few tutorials out there about these technologies in various scenarios and combinations, but I've been having trouble finding clear help specifically for Core 2.0 due to how recently it was released, and I'm avoiding getting too invested in any of the Core 1.x tutorials because it seems there have been some breaking changes when it comes to this (JWT, authentication, etc.). I'm entirely new to Core, so I can't tell what's what.

My goal is to ascertain how this is supposed to be done according to Microsoft's suggestions/standards (if they exist). I want to minimize complexity and make use of the tools that have been designed for this ecosystem.


I have registered both the Web App and the Web API in my Azure Active Directory. When I debug my Web App, I am required to log in via my work/school account with Microsoft, and that is working as expected.

This is all unmodified from what was created as a result of my using the templates/wizards to get started, but for reference:

In the Web App:

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // (other unrelated stuff)

        app.UseAuthentication();

        // (other unrelated stuff)
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
        .AddAzureAd(options => Configuration.Bind("AzureAd", options))
        .AddCookie();

        services.AddMvc();
    }
}

Controllers/HomeController.cs

[Authorize]
public class HomeController : Controller
{
    public IActionResult Index()
    {
        // ****************************************************************
        // let's imagine I wanted to call the Web API right here, passing authentication
        // and whatnot... what should that look like according to this framework?
        // ****************************************************************

        return View();
    }

    // (other unrelated actions)
}

In the Web API:

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        // (other unrelated stuff)

        app.UseAuthentication();
        app.UseMvc();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
        })
        .AddAzureAdBearer(options => Configuration.Bind("AzureAd", options));

        services.AddMvc();
    }
}
DJ Grossman

I can't think of how many queries I threw into Google until finally trying "c# asp core get access token" and getting this very helpful blog as result #3:

https://blogs.msdn.microsoft.com/gianlucb/2017/10/04/access-an-azure-ad-secured-api-with-asp-net-core-2-0/

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Azure Web App logout from AAD Authentication

分類Dev

Trouble getting ClaimsPrincipal populated when using EasyAuth to authenticate against AAD on Azure App Service in a Asp.Net Core web app

分類Dev

In Azure Service Fabric, what is the deference between a Stateless Web API and the ASP.NET Core Web API?

分類Dev

Web APIのAuthorizeAttribute(ASP.NET Core 2)

分類Dev

Getting all users per application Azure Active Directory .NET Web Api C#

分類Dev

.Net Core 2.0 Web API using JWT - Adding Identity breaks the JWT authentication

分類Dev

Turn existing ASP.NET Core Web App to Edge module running on Azure IoT Edge

分類Dev

Asp Net Core Web Api POSTRequest.Bodyは常に長さ0

分類Dev

Mobile app Web API authentication

分類Dev

Using transactions in ASP.NET Web API

分類Dev

ASP.NET Web API 2 ViewModels?

分類Dev

Cannot launch asp.net core web app in IIS Express

分類Dev

Using a C# 7 tuple in an ASP.NET Core Web API Controller

分類Dev

How to return a 304 result from ASP.NET Core web api?

分類Dev

How to invoke web api method from client side in asp.net core?

分類Dev

Return complete XML response from ASP .NET Core Web API method

分類Dev

How to call an ASP.NET Core Web API endpoint from within separate Docker Containers

分類Dev

Picasso is not loading Image from URL on ASP.NET Core Web API server

分類Dev

ASP.NET Core Web API + Angular2の承認と認証

分類Dev

Validating B2C JWT tokens in Asp.Net Core Web Api

分類Dev

Asp.Net Core 2 Web API shows HTTP 404 error on Visual Studio 2017, Windows 10

分類Dev

ASP.Net Core 2.1 Web Api2とActiveDirectoryの資格情報

分類Dev

ASP.NET Core 2 Web API-502 Bad Gate Way(Centos 7)-NGINX

分類Dev

ASP.NET Core 2.0 Web API Azure Adv2トークン認証が機能しない

分類Dev

ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

分類Dev

Return "raw" json in ASP.NET Core 2.0 Web Api

分類Dev

Unable to get request header in asp net core web API

分類Dev

ASP.NET Core Web APIの例外処理

分類Dev

ASP.NET Core Web API認証

Related 関連記事

  1. 1

    Azure Web App logout from AAD Authentication

  2. 2

    Trouble getting ClaimsPrincipal populated when using EasyAuth to authenticate against AAD on Azure App Service in a Asp.Net Core web app

  3. 3

    In Azure Service Fabric, what is the deference between a Stateless Web API and the ASP.NET Core Web API?

  4. 4

    Web APIのAuthorizeAttribute(ASP.NET Core 2)

  5. 5

    Getting all users per application Azure Active Directory .NET Web Api C#

  6. 6

    .Net Core 2.0 Web API using JWT - Adding Identity breaks the JWT authentication

  7. 7

    Turn existing ASP.NET Core Web App to Edge module running on Azure IoT Edge

  8. 8

    Asp Net Core Web Api POSTRequest.Bodyは常に長さ0

  9. 9

    Mobile app Web API authentication

  10. 10

    Using transactions in ASP.NET Web API

  11. 11

    ASP.NET Web API 2 ViewModels?

  12. 12

    Cannot launch asp.net core web app in IIS Express

  13. 13

    Using a C# 7 tuple in an ASP.NET Core Web API Controller

  14. 14

    How to return a 304 result from ASP.NET Core web api?

  15. 15

    How to invoke web api method from client side in asp.net core?

  16. 16

    Return complete XML response from ASP .NET Core Web API method

  17. 17

    How to call an ASP.NET Core Web API endpoint from within separate Docker Containers

  18. 18

    Picasso is not loading Image from URL on ASP.NET Core Web API server

  19. 19

    ASP.NET Core Web API + Angular2の承認と認証

  20. 20

    Validating B2C JWT tokens in Asp.Net Core Web Api

  21. 21

    Asp.Net Core 2 Web API shows HTTP 404 error on Visual Studio 2017, Windows 10

  22. 22

    ASP.Net Core 2.1 Web Api2とActiveDirectoryの資格情報

  23. 23

    ASP.NET Core 2 Web API-502 Bad Gate Way(Centos 7)-NGINX

  24. 24

    ASP.NET Core 2.0 Web API Azure Adv2トークン認証が機能しない

  25. 25

    ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

  26. 26

    Return "raw" json in ASP.NET Core 2.0 Web Api

  27. 27

    Unable to get request header in asp net core web API

  28. 28

    ASP.NET Core Web APIの例外処理

  29. 29

    ASP.NET Core Web API認証

ホットタグ

アーカイブ