[英]Asp.Net Core 2.1 SPA React Template with Windows Authentication
When I use Windows Authentication in my debug environment(IIS Express), I get the proper domain name. 在调试环境(IIS Express)中使用Windows身份验证时,将获得正确的域名。 But when I push it to the production environment(IIS), I get no domain name at all.
但是,当我将其推送到生产环境(IIS)时,根本没有域名。 Am I missing something?
我想念什么吗?
Reproduce my Issue: 重现我的问题:
I've created a new Web Project in VS2017 (15.7.6) with the React template and enabled Windows authentication by changing the launchSettings.json to: 我已使用React模板在VS2017(15.7.6)中创建了一个新的Web项目,并通过将launchSettings.json更改为以下方式启用了Windows身份验证:
"windowsAuthentication": true,
"anonymousAuthentication": false,
Now I changed the ConfigureServices
Method in Startup.cs
to this: 现在,我将
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";
});
}
I added 2 Files to the Project: 我向项目添加了2个文件:
CheckForEmployee.cs 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 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;
}
}
}
Anyone facing a similar problem, make sure Windows Authentication is enabled on your IIS Service. 任何面临类似问题的人,请确保在IIS服务上启用了Windows身份验证。 This solved my problem.
这解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.