0

How can I get the current logged in user in SharePoint in an added module?

I have tried the following codes but I always get the system account

   SPUser user = null;

    using (SPSite site = new SPSite(@"siteName"))
    {

        using (SPWeb web = site.OpenWeb())
        {

            user = web.CurrentUser;
        }
    }

 //and

 SPContext.Current.Web.CurrentUser

 //and

  public SPUser GetUserWithElevated()
  {
    SPUser user = null;

    SPSecurity.CodeToRunElevated elevatedSubmit = new 
    SPSecurity.CodeToRunElevated(delegate
    {

         user = SPContext.Current.Web.CurrentUser;
    });
    SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);

    return user;
 }


//this is my Httpmodule: 

 public void Init(HttpApplication context)
{
    context.PreRequestHandlerExecute += new 
 EventHandler(context_PreRequestHandlerExecute);
 }

 void context_PreRequestHandlerExecute(object sender, EventArgs e)
 {
    Page page = HttpContext.Current.CurrentHandler as Page;
    if (page != null)
    {
        // register handler for PreInit event
        page.PreInit += new EventHandler(page_PreInit);
    }
 }

 void page_PreInit(object sender, EventArgs e)
 { //user interception goes here}

2 Answers 2

0

already answered here:

Get current logged user name for user "Account operates as System"

also like to note:

  public SPUser GetUserWithElevated()
  {
    SPUser user = null;

    SPSecurity.CodeToRunElevated elevatedSubmit = new 
    SPSecurity.CodeToRunElevated(delegate
    {

         user = SPContext.Current.Web.CurrentUser;
    });
    SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);

    return 
  }

your running under the system account ;)

also like to note if the solution in my other answer doesnt work as i presume it doesnt... that could be because your account is running under a system account? when you log into sharepoint what user name is displayed?

under module this should work:

HttpContext.Current.User.Identity.Name

if that doesnt work than try the spcontext method:

SPContext.Current.Web.CurrentUser.LoginName

as i said, goto the url on the browser to first make sure the account your using is not set as a system account. Runwithelevated priv uses the app pool account which also shows as system account! In the code you provided is that three methods you have tried that all give system accounts?

You need to test with a dummy account to make sure its not the account your currently using that is the issue

EDIT

if your using the windows identity to login users into sharepoint than you can do:

string getCurrentWindowsUserName = WindowsIdentity.GetCurrent().Name;

http://msdn.microsoft.com/en-us/library/vstudio/sfs49sw0(v=vs.100).aspx

returning different account types depends on your settings you have from web.config to code like you have above (elvated priv) to sharepoint central admin (setting account as system account):

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

EDIT

ok so based on your answer below i can say that the code is working as its passing the correct value! the place its going wrong could be in two places!

1) goto central admin and check the webapplication user policy. Click on the user and see if the tickbox run as system account is checked on a user you know is not ment to be run as system account.

i have a strong feeling the above is not the issue as you can have local accounts created on sharepoint site that are also showing the same issue? so based on this presumption follow number 2..

2) goto iis manager -> goto the site in question (click on the site) -> on the right hand side click on Advanced options -> a popup will appear -> make sure "physical path credentials" is empty.... click on the button '...' and you should get a popup -> make sure you have application user is checked and click ok!

hopefully above should solve your issue!

4
  • i suffer from this problem during a couple of days even i log with an account other than system with a simple user with an another session but on the same machine i got the system account always.
    – ezzaam
    May 27, 2014 at 9:57
  • so any account you use to log into sharepoint you see system account rather than the user account name?
    – Ali Jafer
    May 27, 2014 at 10:02
  • thanks HttpContext.Current.User.Identity.Name; and SPContext.Current.Web.CurrentUser.LoginName; helped me
    – ezzaam
    May 27, 2014 at 10:11
  • no problem happy to help!
    – Ali Jafer
    May 27, 2014 at 10:13
0

Try following:

HttpContext.Current.User.Identity.Name
7
  • also i got the system account i do not know whether i'm running web site on the same machine where iis exist or anything else
    – ezzaam
    May 27, 2014 at 9:19
  • so where are you running your HTTP module code?
    – Aanchal
    May 27, 2014 at 9:22
  • in : void page_PreInit(object sender, EventArgs e) { //code goes here}
    – ezzaam
    May 27, 2014 at 9:24
  • So on which machine are you running this code? On the same machine where sharepoint is installed or somewhere else?
    – Aanchal
    May 27, 2014 at 9:27
  • on the same machine
    – ezzaam
    May 27, 2014 at 9:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.