1

Here is the problem, I am using IIS7 inorder to host my asp.net web site , Now the thing is no matter what method is used, I am always getting the app pool identity as the current logged on user (authentication mode is windows). The following are the pieces of code i used to get the logged on user(all of them return the app pool identity).

WindowsIdentity.GetCurrent().Name 
Convert.ToString(Request.ServerVariables["LOGON_USER"])
WindowsPrincipal p.Identity.Name
Convert.ToString(Request.ServerVariables["AUTH_USER"]).Trim()

Please help me regarding this cause this is troubling me a lot.

abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

2

You should use asp.net impersonation:

http://support.microsoft.com/kb/306158

http://msdn.microsoft.com/en-us/library/aa292118%28v=vs.71%29.aspx

http://msdn.microsoft.com/en-us/library/ff647076.aspx

<identity impersonate="true" />

---UPDATE----

look at IIS configuration:

ASP.NET impersonation problem

----Update 2 ----

    public string GetLoggedUserName()
    {
        string rtn = string.Empty;

        if (CurrentContext.User != null)
        {
            if (CurrentContext.User.Identity.IsAuthenticated)
            {
                var gp = CurrentContext.User as WindowsIdentity;
                if (gp!=null)
                {                
                    rtn = gp.Identity.Name;
                }
            }
        }
        return rtn;
    }
Community
  • 1
  • 1
giammin
  • 18,620
  • 8
  • 71
  • 89
  • @JaydeepGhose look at the answer of that question http://stackoverflow.com/questions/5985153/asp-net-impersonation-problem – giammin Jan 11 '13 at 09:42
  • If impersonate is enabled, wont it impersonate the logged in user id in place of the service account. That means it will try to access resources using the logged in user account. This may be a problem because logged in users may not have adequate permissions to carry out server tasks like the app pool account has . Is my understanding correct? – Jaydeep Ghose Jan 11 '13 at 10:08
  • @JaydeepGhose yes, you should use impersonation only if you want that behavior – giammin Jan 11 '13 at 11:12
  • ya thats why I havent used , so isnt there any way of getting the login user if impersonation is not used and windows authetication is used – Jaydeep Ghose Jan 11 '13 at 11:18
  • @JaydeepGhose try to cast to WindowsIdentity instead of GenericPrincipal – giammin Jan 11 '13 at 12:12
  • Its a type cast problem, you had used GenericIDentity , we need to use WindowsIdentity. BNut still the service account is being fetched – Jaydeep Ghose Jan 11 '13 at 12:13
  • @JaydeepGhose this is very strange... try this: (WindowsPrincipal) Thread.CurrentPrincipal(); – giammin Jan 11 '13 at 12:33
  • Yes that also I have used, do you think this is somewhat related to the web.config settings or the IIS settings? – Jaydeep Ghose Jan 11 '13 at 12:47
  • @JaydeepGhose are you using IE? did you check this IIS config: http://i.stack.imgur.com/UXthB.png – giammin Jan 11 '13 at 14:25
1

Thanks for all the information. The problem was due to some kind of bug in the ASP.NET , What I was doing is that I was redirecting from default.aspx to Home.aspx in the web application but the problem was that after redirection some how the logged in user information was being lost. So I changed the home page to Home.aspx and stored all the required information there and now it is working perfectly.

Thanks giammin for your help , really appreciate it/.