Retrieving the current username in an ASP.NET application

I am launching a webpage which should be able to read the login ID of the current user. Here is the code I'm using:

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

This currently returns the correct login, but when I use it in this method:

protected Boolean isPageOwner()
{
    string id = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
    alert("User: " + id);
    if (id.Equals(pageOwnerID))
    {
        return true;
    }
    if (accessPermission.ContainsKey(id))
    {
        return true;
    }
    return false;
}

The method returns false, even if the returned identifier is identical to pageOwnerID. I'm really not sure which part of this I have a problem in.

On a side note, my login ID is of the form string1 / string2, but the code returns it as string1 + string2 without a slash.

Any advice is appreciated.

Sincerely.

+5
source share
2 answers

Try using this to get your username ....

if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
{
   string username = System.Web.HttpContext.Current.User.Identity.Name;
}

, Windows - Windows.

web.config...

<system.web>
 <authentication mode="Windows"/>
  <authorization>
    <deny users="?"/> 
  </authorization>
</system.web>
+8

( Project ), :

string userId = Thread.CurrentPrincipal.Identity.GetUserId();

0

All Articles