Mvc 4自动填充混合模式身份验证中的Windows用户名 [英] Mvc 4 auto fill windows user name in mixed mode authentication

查看:94
本文介绍了Mvc 4自动填充混合模式身份验证中的Windows用户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ASP.NET MVC 4中的混合身份验证模式,我希望在我的文本框中自动填充Windows用户名。我使用过System.Security.Principal.WindowsIdentity.GetCurrent()。Name;它在本地工作正常,但当我在服务器上执行它时,默认AppPool将出现在用户名文本框中。

请建议一些approch。

谢谢提前



我的尝试:



@model

@ {

ViewBag.Title =活动目录认证;

}

@ {

string UserIDwindows = System.Security.Principal.WindowsIdentity.GetCurrent()。Name;

string [] Usernameis = UserIDwindows.Split('\\');

if(ViewData [Error]!=&& ViewData [Error]!= null)

{

string res = ViewData [Error]。ToString();







@res





ViewData [Error] = null;



}

}



@using(Html.BeginForm(in dex,Activedirectory))

{

输入您的网络/系统登录证书



<表格宽度=100%> @ Html.LabelFor(a => a.UserID)



@ Html.TextBoxFor(a => a.UserID,new {@ Value = Usernameis [1]})

@ * @ Html.TextBox(UserID,)* @



@ Html.ValidationMessageFor(a => a.UserID)



@ Html.LabelFor(a => a.Password)



@ Html.TextBoxFor(a => a.Password)



@ Html.ValidationMessageFor(a => a.Password)



< td> @ Html.LabelFor(a => a.DomainName)



@ Html.TextBoxFor( a => a.DomainName)



@ Html.ValidationMessageFor(a => a.DomainName)







}

I am working on mixed authentication mode in ASP.NET MVC 4, and I want the Windows username to be auto filled in my textbox. I have used System.Security.Principal.WindowsIdentity.GetCurrent().Name; and it is working fine on local, but when i am executing it on the server, "Default AppPool " is coming in username textbox.
Please suggest some approch.
thanks in advance

What I have tried:

@model
@{
ViewBag.Title = "Active directory authentication";
}
@{
string UserIDwindows = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
string[] Usernameis = UserIDwindows.Split('\\');
if (ViewData["Error"] != "" && ViewData["Error"] != null)
{
string res = ViewData["Error"].ToString();



@res


ViewData["Error"] = null;

}
}

@using (Html.BeginForm("index", "Activedirectory"))
{

ENTER YOUR NETWORK / SYSTEM LOGIN CREDENTIALS


@Html.LabelFor(a => a.UserID)

@Html.TextBoxFor(a => a.UserID, new { @Value = Usernameis[1] })
@* @Html.TextBox(UserID,)*@

@Html.ValidationMessageFor(a => a.UserID)

@Html.LabelFor(a => a.Password)

@Html.TextBoxFor(a => a.Password)

@Html.ValidationMessageFor(a => a.Password)

@Html.LabelFor(a => a.DomainName)

@Html.TextBoxFor(a => a.DomainName)

@Html.ValidationMessageFor(a => a.DomainName)



}

推荐答案

您获得默认应用程序池的原因是 System.Security.Principal .WindowsIdentity.GetCurrent()。姓名



当你在本地跑步时,我假设你的意思是视觉工作室,这就是你看到的原因您的用户名。您的用户正在运行Visual Studio,它启动了IE Express,这就是您使用页面的方式。



如果您要将该站点移动到本地的完整IIS上计算机它可能会做同样的事情。



这很可能是因为你需要禁用它时,你的应用程序中启用了匿名身份验证。当您在IIS上找到它时,请转到IIS中的站点,单击< site name>下的身份验证图标。家。然后你应该看到一个auth类型列表。您将看到匿名身份验证可能已设置为启用,其他所有内容都已禁用。您需要禁用匿名身份验证并启用Windows身份验证。



然后,要访问用户信息,您应该能够使用 User.Identity 。*** ..例如 User.Identity.UserName 或类似的东西。



自从我在MVC中完成windows auth以来已经有一段时间了,所以我认为你需要查看一些链接以获得更多说明。



如何:在ASP.NET 2.0中使用Windows身份验证 [ ^ ]
The reason why you get default app pool is because of System.Security.Principal.WindowsIdentity.GetCurrent().Name.

When you are running locally i am assuming you mean from visual studio which is why you see your username. Your user is running visual studio, which launched IE Express which is how you work with your page.

If you were to move that site to full blown IIS locally on your computer it will probably do the same thing.

This is more than likely because you've enabled anonymous authentication in your app when you need to have it disabled. When you've got it out on IIS go to the site in IIS, click the Authentication icon under <site name> Home. Then you should see a listing of auth types. You'll see Anonymous Authentication is probably set to enable and everything else is Disabled. You'll want to disable anonymous auth and enable windows auth.

Then, to access user info you should be able to use User.Identity.***..for example User.Identity.UserName or something like that.

It has been a while since i've done windows auth in MVC so I think you need to look some links for more clarification.

How To: Use Windows Authentication in ASP.NET 2.0[^]


这篇关于Mvc 4自动填充混合模式身份验证中的Windows用户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆