Tas*_*sto 28 c# asp.net windows-server-2008
要获取系统中当前登录的用户,我使用以下代码:
string opl = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
Run Code Online (Sandbox Code Playgroud)
我在ASP.NET应用程序上工作,我需要这些信息.所以我把我的应用程序放在服务器上并尝试上面的代码,然后在字符串opl中得到"网络服务".我需要知道访问我的ASP.NET应用程序的PC的当前用户.
Ben*_*nCr 52
快速回答是 User = System.Web.HttpContext.Current.User
确保您的web.config具有以下身份验证元素.
<configuration>
<system.web>
<authentication mode="Windows" />
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
Run Code Online (Sandbox Code Playgroud)
进一步阅读:配方:在Intranet ASP.NET Web应用程序中启用Windows身份验证
小智 21
使用System.Web.HttpContext.Current.User.Identity.Name应该工作.请执行以下操作,检查托管您站点的服务器上的IIS站点设置:
转到IIS→ 站点 →您的站点→ 身份验证

现在检查是否已禁用匿名访问并启用了Windows身份验证.

现在System.Web.HttpContext.Current.User.Identity.Name应该返回这样的东西:
domain\username
Rob*_*ert 17
如果您使用会员资格,您可以: Membership.GetUser()
您的代码将返回分配有ASP.NET的Windows帐户.
最好的做法是首先检查Identity.IsAuthenticated属性,然后得到usr.UserName如下信息:
string userName = string.Empty;
if (System.Web.HttpContext.Current != null &&
System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
System.Web.Security.MembershipUser usr = Membership.GetUser();
if (usr != null)
{
userName = usr.UserName;
}
}
Run Code Online (Sandbox Code Playgroud)
小智 5
您可以简单地使用页面的属性。有趣的是,您可以在代码中的任何位置访问该属性。
用这个:
HttpContext.Current.User.Identity.Name
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
182481 次 |
| 最近记录: |