How can I get Current Logged in user of SharePoint Site in Provider Hosted App's code behind?
I tried this:

  1. clientContext.Load(clientContext.Web, web => web.CurrentUser.LoginName);
     var login_nm = clientContext.Web.CurrentUser.LoginName;
  2. userName = Request.LogonUserIdentity.Name.ToString();

  3. string u_nm = User.Identity.Name.ToString();

But what I am getting is current System User. Not the SharePoint user. Above all the Options are to get the Windows User Identity. Not for SharePoint site. What should I do to get SharePoint user in my App.


This should return the current user: (Works for me here)

Web web = clientContext.Web;

currentUser = clientContext.Web.CurrentUser.LoginName;
  • Above My code is somewhat same as yours. But did you try to create user in SharePoint. And not in System User. I mean to say I am getting my System users only. Because my app is running on localhost when I debug it. but how to get SharePoint user in that. – Rahul Gokani May 31 '13 at 14:00
  • I'm not quite understanding your reply question. That is the SharePoint user who I am connected to and using the app as. Are you elevating anything? – Hugh Wood May 31 '13 at 14:22
  • I am telling you that when I run my I from VS in FireFox browser I need to Login twice. First is for my SharePoint site and other is for my Localhost. So when App is running I am getting the localhost user that is System user. And when I run on IE it always give me the System User from which I am logged in. – Rahul Gokani Jun 1 '13 at 4:16
  • Does your app run on Localhost..? Or SharePoint server..? – Rahul Gokani Jun 1 '13 at 11:44
  • 1
    Ohk. My app is running on LocalHost... I think this will be the reason that I am getting the local users in my app. – Rahul Gokani Jun 3 '13 at 8:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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