当我在调试环境(IIS Express)中使用Windows身份验证时,将获得正确的域名。但是,当我将其推送到生产环境(IIS)时,根本没有域名。我想念什么吗?
再现我的问题:
我已经在React 2017 VS2017(15.7.6)中创建了一个新的Web项目,并通过将launchSettings.json更改为以下方式启用了Windows身份验证:
"windowsAuthentication": true,
"anonymousAuthentication": false,
现在,我将ConfigureServices
中的Startup.cs
方法更改为:
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;
}
}
}
答案 0 :(得分:0)
任何面临类似问题的人,请确保在IIS服务上启用了Windows身份验证。这解决了我的问题。