2

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". I need the substring to remove the domain name before the user.

Ryan S
  • 3,210
  • 17
  • 48
  • 79
  • 2
    Maybe I'm misunderstanding, but it sounds like you used asp.net membership authentication locally and the server has windows authentication turned on? – AndrewC Sep 20 '11 at 12:05
  • 1
    Why don't you get the user name from the HttpContext? More http://stackoverflow.com/questions/1574554/how-to-utilize-asp-net-current-user-name-in-sqlparameter-without-code-behind. – Karel Frajták Sep 20 '11 at 12:08
  • @AndyC I updated the description with the code I was using, and now it's not. – Ryan S Sep 20 '11 at 12:08

2 Answers2

6

use

User.Identity.Name

instead of HeadLoginName function.

loginName.Text = User.Identity.Name;
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
xenedia
  • 314
  • 2
  • 9
3

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 ?

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. The above code has always worked for me.

Menno

Menno van den Heuvel
  • 1,851
  • 13
  • 24
  • Nailed it mate, there was no particular reason of trying to use the LoginName, just wanted to get the username and since that was kind of getting it someway, I figured I could, but this is a better fix! – Ryan S Sep 20 '11 at 12:12