简体   繁体   English

获取登录用户ASP.NET MVC的用户名

[英]Get username of logon user ASP.NET MVC

I want to get the username of the logon username and domain. 我想获取登录用户名和域的用户名。 My code for getting it, is in a controller: 获取它的代码是在控制器中:

User.Identity.GetUserId()

In my web.config is: 在我的web.config中是:

<system.web>
    <identity impersonate="false"/>
    <authentication mode="Windows" />
    <compilation debug="true" targetFramework="4.6.1" />
    <httpRuntime />
    <pages controlRenderingCompatibilityVersion="4.0" />
</system.web>

Currently, I only get a empty string. 目前,我只得到一个空字符串。 What I have to change to get the Windows username information? 我必须更改以获取Windows用户名信息?

Sidenote for others : While my research, I also got to the following: 其他人的旁注 :在我的研究中,我还得到以下内容:

Environment.UserDomainName +  @"\" + Environment.UserName

In reference to this , it only delivers the identity in which the thread is running and not the windows information. 参考这个 ,它只传递线程运行的身份而不是窗口信息。

EDIT1: I'm currently testing my program in debugging mode. 编辑1:我目前正在调试模式下测试我的程序。

try this 试试这个

HttpContext.User.Identity.Name

also make sure you have authorization tag in your system.web in web.config as 还要确保在web.config中的system.web中有授权标记

<authorization>
  <allow users="?" />
</authorization>

and in IIS make sure that you will disable annonymous authentication and enable windows for the same app as 并在IIS中确保您将禁用匿名身份验证并为同一个应用程序启用Windows

在此输入图像描述

User.Identity.GetUserId() is an extension method from Microsoft.AspNet.Identity . User.Identity.GetUserId()Microsoft.AspNet.Identity的扩展方法。 Identity is incompatible with Windows Auth (you have to use one or the other), so I'm not sure what you're actually doing here. 身份与Windows Auth不兼容(你必须使用其中一个),所以我不确定你在这里做了什么。

Generally speaking, in Windows Auth, the value of User.Identity.Name will be in the form of {DOMAIN}\\{USER} . 一般来说,在Windows Auth中, User.Identity.Name的值将采用{DOMAIN}\\{USER} So if you wanted the domain and/or username, you can split that string on \\ and take the part you're after. 因此,如果您想要域名和/或用户名,您可以将该字符串拆分为\\并获取您所追求的部分。

When it comes to Identity, User.Identity.Name should be the value of ApplicationUser.UserName , but depending on the setup, it might be ApplicationUser.Email . 说到Identity, User.Identity.Name应该是ApplicationUser.UserName的值,但是根据设置,它可能是ApplicationUser.Email

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM