获取SharePoint添加HttpModule中用户登录用户的当前
-
28-09-2020 - |
题
如何在添加的模块中获取在SharePoint中登录用户的当前?
我已经尝试了以下代码,但我总是得到系统帐户
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}
. 解决方案
已经在此处回答:
还要注意:
public SPUser GetUserWithElevated()
{
SPUser user = null;
SPSecurity.CodeToRunElevated elevatedSubmit = new
SPSecurity.CodeToRunElevated(delegate
{
user = SPContext.Current.Web.CurrentUser;
});
SPSecurity.RunWithElevatedPrivileges(elevatedSubmit);
return
}
.
您在系统帐户下运行;)
还要注意如果我的其他答案中的解决方案并没有工作,因为我认为它不起...这可能是因为您的帐户在系统帐户下运行?登录SharePoint时,将显示用户名的内容?
在模块下,这应该是:
HttpContext.Current.User.Identity.Name
.
如果它不起作用而不是尝试spcontext方法:
SPContext.Current.Web.CurrentUser.LoginName
.
正如我所说,转到浏览器上的URL首先确保您使用的帐户未设置为系统帐户。 RunWithelevated riv使用App Pool帐户,也显示为系统帐户!在您提供的代码中,您尝试过的三种方法所有提供系统帐户?
您需要使用虚拟帐户进行测试,以确保它不是您当前使用的帐户是问题
编辑
如果您使用Windows身份登录用户进入SharePoint之上可以执行以下操作:
string getCurrentWindowsUserName = WindowsIdentity.GetCurrent().Name;
.
http:// msdn。 microsoft.com/en-us/library/vstudio/sfs49sw0(v=vs.100).aspx
返回不同的帐户类型取决于您的设置,您将从Web.config从上面(Elvated rivated)到SharePoint Central Admin(作为系统帐户的设置帐户):
http://msdn.microsoft.com/en-us/library / aaa302377.aspx
编辑
好的,基于下面的答案,我可以说代码正在作为它传递正确的价值!它出错的地方可能在两个地方!1)转到Central Admin并检查WebApplication用户策略。单击用户,看看标记箱是否在您知道的用户身上检查作为系统帐户的用户,您不知道不作为系统帐户运行。
我有一个强烈的感觉,上面不是问题,因为您可以在SharePoint网站上创建的本地帐户也显示同样的问题?因此,基于此推定,请按照2号。/ / p>
2)转到IIS管理器 - >转到问题中的网站(点击站点) - >在右侧单击高级选项 - >将出现一个弹出窗口 - >确保“物理路径凭据”为空。 ...单击“...”按钮,您应该得到一个弹出窗口 - >确保检查应用程序,然后单击“确定”!
希望上面应该解决你的问题!
其他提示
尝试以下:
HttpContext.Current.User.Identity.Name
.