Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to get the current user identity value

I have a intranet website where I am able to get the user's identity value when run in Local machine.

System.Web.HttpContext.Current.User.Identity.Name.ToString().Substring(3)

But When I deploy the same on IIS 8.5, It finds it blank.

Please help me understand where I am going wrong ?

For impersonation we have to use the specific username and password.

Web.config:

<system.web>
    <authentication mode="Windows" />
        <customErrors mode="RemoteOnly" defaultRedirect="~/Pages/Error/Error.aspx" >
            <error statusCode="404" redirect="~/Pages/Error/404.aspx" />
        </customErrors>
    <identity impersonate="true" userName="user1" password="pass1" />
  </system.web>
<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <validation validateIntegratedModeConfiguration="false" />
</system.webServer>

IIS settings:

Windows authentication - Enabled
Impersonation  - Enabled
Rest all disabled.

Default app pool - Integrated mode.
like image 732
Akshay Avatar asked Feb 09 '18 12:02

Akshay


People also ask

How do I find HttpContext current user identity Name?

Web namespace (which contains the HttpContext class), you can reference the members of HttpContext on an . aspx page without using the fully qualified class reference to HttpContext. For example, you can use User.Identity.Name to get the name of the user on whose behalf the current process is running.

How do I get current user in .NET core Web API?

AddTransient<IPrincipal>(provider => provider. GetService<IHttpContextAccessor>(). HttpContext. User);

Why is user identity isauthenticated false?

identity. isauthenticated is False when a user is already logged in.


1 Answers

IIS settings should be like this:

+------------------------------------+
|           Authentication           |
+------------------------------------+
Name                         Status
------------------------     --------
Anonymous Authentication     Disabled ---+
ASP.NET Impersonation        Enabled     |
Basic Authentication         Enabled     |__ Both are important
Digest Authentication        Disabled    |
Forms Authentication         Disabled    |
Windows Authentication       Enabled  ---+

Use User.Identity.Name to get the logon user and test like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        Page.Title = "Home page for " + User.Identity.Name;
    }
    else
    {
        Page.Title = "Home page for guest user.";
    }
}
like image 85
stefan Avatar answered Oct 10 '22 23:10

stefan