c# - 在 ASP.NET Core 2.0 中使用 Azure Active Directory 身份验证从 Web 应用程序到 Web API

标签 c# authentication azure-active-directory asp.net-core-2.0 asp.net-core-webapi

我尝试将 Azure Active Directory 凭据从 ASP.NET Core 2.0 Web 应用传递到 ASP.NET Core 2.0 Web API,以便 Web API 可以根据用户的属性和权限使用react。

诚然,有很多关于这些技术在各种场景和组合中的教程,但由于 Core 2.0 刚刚发布,我一直无法找到明确的帮助特别发布后,我避免对任何 Core 1.x 教程投入太多,因为在这方面似乎发生了一些重大变化(JWT、身份验证等)。我对 Core 完全陌生,所以我不知道什么是什么。

我的目标是确定如何根据 Microsoft 的建议/标准(如果存在)来完成此操作。我希望最大限度地降低复杂性并利用专为该生态系统设计的工具。


我已在 Azure Active Directory 中注册了 Web 应用程序和 Web API。当我调试 Web 应用程序时,我需要通过 Microsoft 的工作/学校帐户登录,并且这按预期工作。

这与我使用模板/向导开始时创建的内容没有任何修改,但仅供引用:

在网络应用程序中:

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)
}

在 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();
    }
}

最佳答案

我无法想象我向 Google 提出了多少查询,直到最终尝试“c# asp core get access token”并获得这个非常有用的博客作为结果#3:

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

关于c# - 在 ASP.NET Core 2.0 中使用 Azure Active Directory 身份验证从 Web 应用程序到 Web API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47825636/

相关文章:

azure - 针对多个 Azure 广告对 Web 应用程序进行身份验证?

azure - 如何将环境相关文本添加到 Azure AD 登录页面

c# - 表示时间段的最有效方法

c# - Gembox - 获取 ExcelCell 的地址

具有接口(interface)类型约束的 C# 泛型方法

c# - 如何使用 EPPlus Excel Package Plus 删除空白行

ubuntu - 无法将 Anypoint 平台身份验证添加到 Ubuntu 中的 Anypoint Studio

node.js - 是否可以使用 SAML2 在 ReactJs 应用程序中实现 SSO?如果可能怎么办?

authentication - Google Developers Console - Oauth 同意屏幕配置不保存输入

azure - 如何授予对 azure 注册应用程序的订阅访问权限?