简体   繁体   English

asp.NET - 在服务器上获取用户名

[英]asp.NET - Get Username while on the Server

I have a small problem, I have an application which used to work perfectly locally. 我有一个小问题,我有一个应用程序,曾经在本地完美工作。 The authentication was being done from a system I built myself, it was basically using the username of the logged in user, but when deploying on the server I ran into a problem because it was using the servers username instead of the user which is trying to access it. 身份验证是从我自己构建的系统完成的,它基本上是使用登录用户的用户名,但是在服务器上部署时遇到了问题,因为它使用的是服务器用户名而不是用户试图访问它。

This was the code which was working, but now it's not on the server: 这是正在运行的代码,但现在它不在服务器上:

    public static string GetUser()
    {
        WindowsIdentity curIdentity = WindowsIdentity.GetCurrent();
        WindowsPrincipal myPrincipal = new WindowsPrincipal(curIdentity);
        return curIdentity.Name;
    }

However I can see that this code: 但是我可以看到这段代码:

     <LoggedInTemplate>
        <span class="alignLeft">
           <asp:LoginName ID="HeadLoginName" runat="server"/>
        </span>
        <span class="alignRight">
           <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/Logout.aspx"/>
        </span>
     </LoggedInTemplate>

, still used to good login of the user and not the server. ,仍然用于良好的用户登录而不是服务器。 I am now trying to get the value of that user, but I am having problems to do that. 我现在正试图获得该用户的价值,但我遇到了问题。

All I need to do is to get that in a string. 我需要做的就是把它放在一个字符串中。

loginName.Text = HeadLoginName.ToString().Substring(6);

This only returned: ".Web.UI.WebControls.LoginName". 这只返回:“。Web.UI.WebControls.LoginName”。 I need the substring to remove the domain name before the user. 我需要子字符串来删除用户之前的域名。

use 采用

User.Identity.Name

instead of HeadLoginName function. 而不是HeadLoginName函数。

loginName.Text = User.Identity.Name;

What code were you using to try to get the user´s name before? 您之前使用了什么代码来尝试获取用户名? How are your users logging in? 您的用户如何登录?

HttpContext.Current.User.Identity.Name ? HttpContext.Current.User.Identity.Name?

I´m not sure if you could ever pull the user´s login name out of the LoginName web control (I suspect not), but I suggest finding a less roundabout way of doing it. 我不确定你是否可以从LoginName网页控件中取出用户的登录名(我怀疑没有),但我建议找一个不那么迂回的方法。 The above code has always worked for me. 上面的代码一直对我有用。

Menno 门诺

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

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