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