SPContext.Current.Web.CurrentUser returns misleading value

后端 未结 5 1746
没有蜡笔的小新
没有蜡笔的小新 2021-01-03 02:02

I\'m trying to find out current user name for my sharepoint application. There are more that one way how to do this. However the sharepoint way returns misleading value.

5条回答
  •  长情又很酷
    2021-01-03 02:28

    The problem is because you are probably getting the current user from an elevated SPWeb inside a RunWithElevatedPrivileges code. You can use the snippet below to get the real user

    SPWeb site = SPContext.Current.Web;
    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
        using (SPSite ElevatedsiteColl = new SPSite(siteColl.ID))
       {
           using (SPWeb ElevatedSite = ElevatedsiteColl.OpenWeb(site.ID))
           {
                string currUser = site.CurrentUser; //not the ElevatedSite.CurrentUser
           }
       }
    });
    

    This will show the real user name instead of the SHAREPOINT\System user.

提交回复
热议问题