I有一个由Visual Studio 2022制成的默认React Spa页面,并使用Windows身份验证。我的应用仅在IIS托管的Intranet应用程序上运行。
在我的program.cs,我配置了身份验证和授权:
builder.Services.AddControllersWithViews();
builder.Services.AddAuthentication();
builder.Services.AddAuthorization();
builder.Services.AddHttpContextAccessor();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = NegotiateDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = NegotiateDefaults.AuthenticationScheme;
}).AddNegotiate();
app.UseAuthentication();
app.UseAuthorization();
在测试中,我创建了一个方法应用程序。使用以捕获用户名,当我使用iisexpress配置文件运行时,我可以在主页上成功获取记录的Windows用户
if子句上的断点在应用程序上的每个请求中都键
app.Use(async (context, next) =>
{
if (!context.User.Identity.IsAuthenticated)
{
await context.ChallengeAsync();
await next();
}
else
{
await next();
}
});
但是,当我调用示例“ WeatherForecastController”示例时,或者我没有在httpcontext上获得记录的用户,或者页面向我发送了错误401.2-未经授权。我还设置了在获取呼叫上的凭据,以“ include”
async populateWeatherData() {
const response = await fetch('weatherforecast', { "credentials": "include" });
const data = await response.json();
this.setState({ forecasts: data, loading: false });
}
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
"iisExpress": {
"applicationUrl": "http://localhost:53168",
"sslPort": 44381
}
也是为了将inprocess更改为OutofProcess构建,并且发生相同的错误
我也创建了一个mini web.config,以设置安全标签,以允许Windows身份验证并试图调整.VS文件夹.vs。