use*_*063 2 authentication asp.net-mvc asp.net-mvc-4
我一直在尝试获取当前的Windows用户名,但最后没有.以下是我尝试但无法获取用户名的方法,我得到空值
STEP我试过了
string usr System.Web.HttpContext.Current.User.Identity.Name;
Run Code Online (Sandbox Code Playgroud)
得到空值
STEP在安全和启用Windows身份验证下检查我的本地ISS
在我的项目中,我已经选中了单选按钮以使用"使用Visual Studio"开发服务器
在我的web.config文件中,我选择了Form = Windows
<authentication mode="Windows">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
Run Code Online (Sandbox Code Playgroud)STEP:我也尝试过授权
<authentication mode="Windows" >
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
Run Code Online (Sandbox Code Playgroud)STEP:我在我的开发服务器中托管并检查了匿名身份验证=禁用和Windows身份验证=启用
STEP:授权(我在控制器上使用过它)
string str = System.Web.HttpContext.Current.User.Identity.Name
Run Code Online (Sandbox Code Playgroud)
但得到了空值
我已经尝试了所有这些方式来获取当前用户名,但我得到空值.我试图让它在控制器下.有没有办法得到这个?请指教.
设置如下web.config:
<configuration>
....
<system.web>
<authentication mode="Windows" />
....
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
这使Windows身份验证能够获取Windows用户名.
接下来,如果您使用的是Internet Explorer,则需要将" http:// localhost " 添加到有效的Intranet站点列表中(在IE中Internet Options > Security > Local Intranet > Sites > Advanced,然后添加http://localhost到该列表中).这允许IE自动获取当前Windows用户并启动您的网站,而无需再次提示输入用户名/密码.
在你HomeController的Index方法中:
public ActionResult Index()
{
// get the currently logged on Windows user
ViewBag.UserName = HttpContext.User.Identity.Name;
return View();
}
Run Code Online (Sandbox Code Playgroud)
然后在你看来:
<h3>Hello, @ViewBag.UserName !</h3>
Run Code Online (Sandbox Code Playgroud)
而已 !
| 归档时间: |
|
| 查看次数: |
7884 次 |
| 最近记录: |