Question

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}
Was it helpful?

Solution

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!

OTHER TIPS

Try following:

HttpContext.Current.User.Identity.Name
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top