0

How can I get the currently logged in user in SharePoint, I tried:

      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}

I always get the system account, any solution?

7
  • From where are executing this code? From application page, Web part, console app or something else?
    – Aanchal
    May 23, 2014 at 11:33
  • inside workflow i want to intercept the current user when he connect to mys site i do not know what is the right place in web part or....
    – ezzaam
    May 23, 2014 at 11:42
  • actually i want to intercept that in HttpModule i want to add to my site
    – ezzaam
    May 23, 2014 at 11:45
  • any help please
    – ezzaam
    May 23, 2014 at 11:53
  • 1
    You should edit the question to reflect not only what you are doing but how you are trying to do it. Also would be helpful to know how users login to SharePoint...i.e. Forms, Integrated, Claims, etc.
    – Rob D'Oria
    May 23, 2014 at 11:53

1 Answer 1

1

So you mentioned a HttpModule in one of your comments. SPContext.Current.Web.User will work in a SharePoint-deployed HttpModule as long as your module doesn't inspect the SPContext before PreRequestHandlerExecute...i.e. it won't be there in BeginRequest for example.

Assuming you're using integrated authentication the currently logged in user will also be in the HttpContext:

HttpContext context = (sender as HttpApplication).Context;
string userName = context.User.Identity.Name;

I've found that getting it from the HttpApplication (sender argument of event handler) is more universally reliable than HttpContext.Current.User.Identity.Name

1
  • 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) { //here}
    – ezzaam
    May 23, 2014 at 13:31

Your Answer

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

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