在我看来,在apsnet核心MVC项目中,我得到了一个简单的Windows域用户。在谷歌( google )上,我看到很多人都有同样的问题,但在一个完整的auth系统中几乎没有什么建议。我只是希望打开windows并获得一个用户名,不需要任何其他函数就可以在视图中显示名称。
在过去,我曾使用:
@User.Identity.Name在我的launchSettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,我还在Program.cs中添加了:
app.UseAuthentication();
app.UseAuthorization();有没有办法只获取asp.net核心6中显示值的windows用户?
发布于 2022-06-21 07:16:23
在我看来,您应该有身份验证才能获得UserName,您需要启用Windows。
Program.cs:
builder.Services.AddAuthentication(NegotiateDefaults.AuthenticationScheme)
.AddNegotiate();
builder.Services.AddAuthorization(options =>
{
// By default, all incoming requests will be authorized according to the default policy.
options.FallbackPolicy = options.DefaultPolicy;
});
builder.Services.AddHttpContextAccessor();
app.UseAuthentication();
app.UseAuthorization();launchSettings.json:
"iisSettings": {
"windowsAuthentication": true,
"anonymousAuthentication": false,
}主计长:
string UserName = HttpContext.User.Identity.Name;测试结果:

有关更多细节,请查看此链接。
如果您不需要使用Windows自动登录用户,并且您已经有了一个自定义的登录控制器来实现这一点,那么您可以参考这。
https://stackoverflow.com/questions/72692474
复制相似问题