[英]ASP.NET Core 2.1 Custom RoleProvider with Windows Authentication
[英]Asp.Net Core 2.1 SPA React Template with Windows Authentication
在調試環境(IIS Express)中使用Windows身份驗證時,將獲得正確的域名。 但是,當我將其推送到生產環境(IIS)時,根本沒有域名。 我想念什么嗎?
重現我的問題:
我已使用React模板在VS2017(15.7.6)中創建了一個新的Web項目,並通過將launchSettings.json更改為以下方式啟用了Windows身份驗證:
"windowsAuthentication": true,
"anonymousAuthentication": false,
現在,我將Startup.cs
的ConfigureServices
方法更改為:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<IISOptions>(options =>
{
options.AutomaticAuthentication = true;
});
services.AddAuthentication(IISDefaults.AuthenticationScheme);
var names = new[] { "peter", "joey" };
services.AddAuthorization(options =>
{
options.AddPolicy("OnlyEmployees", policy =>
{
policy.AddAuthenticationSchemes(IISDefaults.AuthenticationScheme);
policy.Requirements.Add(new CheckForEmployee(names));
});
});
services.AddSingleton<IAuthorizationHandler, CheckForEmployeeHandler>();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/build";
});
}
我向項目添加了2個文件:
CheckForEmployee.cs
using Microsoft.AspNetCore.Authorization;
namespace WebServer_WindowsAuthentication
{
public class CheckForEmployee : IAuthorizationRequirement
{
public string[] Names { get; set; }
public CheckForEmployee(string[] names)
{
Names = names;
}
}
}
CheckForEmployeeHandler.cs
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
namespace WebServer_WindowsAuthentication
{
public class CheckForEmployeeHandler : AuthorizationHandler<CheckForEmployee>
{
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CheckForEmployee requirement)
{
if (requirement.Names.Contains(context.User.Identity.Name))
{
context.Succeed(requirement);
}
return Task.CompletedTask;
}
}
}
任何面臨類似問題的人,請確保在IIS服務上啟用了Windows身份驗證。 這解決了我的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.