如何将Azure Active Directory身份验证添加到Razor Pages应用程序? [英] How to add Azure Active Directory authentication to a Razor Pages app?
本文介绍了如何将Azure Active Directory身份验证添加到Razor Pages应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
据我所知,您是通过执行New Project>;ASP.NET核心Web应用程序>;[提供应用程序名称]>;Web应用程序来在Visual Studio 2019中创建Razor Pages应用程序
以下教程介绍如何将Azure Active Directory身份验证添加到MVC应用程序。我让样例MVC应用程序正常工作。 我将本教程中的所有必要代码复制到Razor Pages应用程序(Program.cs和Startup.cs)中,但没有收到任何身份验证提示。这是否意味着Razor页面不受支持?还是我做错了什么?https://docs.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp
推荐答案
基本上,您需要在代码中执行以下3项操作。
- 正在对您的ConfigurationServices进行更改。
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
.AddAzureAD(options => Configuration.Bind("AzureAd", options));
services.AddRazorPages().AddMvcOptions(options =>
{
var policy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
options.Filters.Add(new AuthorizeFilter(policy));
});
}
- appsettings.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "<Your Domain>",
"TenantId": "<Your TenantId>",
"ClientId": "<ClientId>",
"CallbackPath": "/signin-oidc"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
- 确保在Startup.cs中具有以下配置方法代码
app.UseAuthentication();
app.UseAuthorization();
或者,如果您使用下面的命令创建DotNet核心应用程序,您将完成所有工作并为您做好准备。
dotnet new razor --auth SingleOrg --client-id <applicationId> --tenant-id <domaintenantid> --domain <domain>
这篇关于如何将Azure Active Directory身份验证添加到Razor Pages应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文