gpt4 book ai didi

asp.net-mvc - MVC 中的 Windows 身份验证

转载 作者:行者123 更新时间:2023-12-03 18:18:00 27 4
gpt4 key购买 nike

我想使用 Windows 身份验证检查用户的登录名。
我有这个作为我的 Controller 的构造函数:

public class HomeController : BaseController
{
public string UserIdentityName;

public HomeController()
{
UserIdentityName = System.Web.HttpContext.Current.User.Identity.Name;// HttpContext.Current.User.Identity.Name;
}
}

但是 UserIdentityName 返回空字符串...

我的 web.config 中也有这个:
<authentication mode="Windows" />   

任何的想法?

最佳答案

在解决方案资源管理器 Pane 中 选择 Web 项目并点击 F4 . (不是右键+属性,那是不同的)

在属性 Pane 中设置:
Windows 身份验证 : 使能够
匿名认证 : 已禁用

在 Web.config 中:<authentication mode="Windows"></authentication>
获取用户名:

HttpContext.Current.User.Identity.Name OR User.Identity.Name

运行你的项目,快乐的日子!

关于asp.net-mvc - MVC 中的 Windows 身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12552950/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com