[英]How to find the logged in user in Sharepoint?
我已經開發了一個“ Web部件”,必須將其部署在Sharepoint服務器上。 我需要用戶的用戶名,該用戶名已在Web部件內登錄到Sharepoint服務器。
如何獲得該用戶名?
以下為我工作:
SPWeb theSite = SPControl.GetContextWeb(Context);
SPUser theUser = theSite.CurrentUser;
string strUserName = theUser.LoginName;
並檢查這出。
您可以使用:
SPWeb web = SPControl.GetContextWeb(this.Context);
string userName = web.CurrentUser.LoginName;
要么
string userName = this.Context.User.Identity.Name;
並且您還應該檢查this.Context.User.Identity.IsAuthenticated
,以確保在嘗試提取用戶名之前有用戶登錄。
大家好,我得到了我的問題的答案希望這對大家都有用...首先在您的Web部件中添加對MicrosoftSharepoint.dll文件的引用。 然后使用Microsoft.SharePoint編寫;
string username;
string sspURL = "URL where Sharepoint is deployed";
SPSite site = new SPSite(sspURL);
SPWeb web = site.OpenWeb();
SPUser user = web.CurrentUser;
username = user.LoginName;
site.AllowUnsafeUpdates = true;
您的Jigar <3
SPContext.Current.Web.CurrentUser
您還可以通過_spPageContextInfo屬性獲取當前記錄的用戶ID。
_spPageContextInfo.userId
您將通過_spPageContextInfo獲得當前用戶的ID。 試試這個可能對您有幫助。
//不要忘記添加System.DirectoryServices.AccountManagement作為參考並使用System.DirectoryServices.AccountManagement;
PrincipalContext insPrincipalContext = new PrincipalContext(ContextType.Domain, "MyDomain","DC=MyDomain,DC=com");
UserPrincipal insUserPrincipal = new UserPrincipal(insPrincipalContext);
insUserPrincipal.Name = "*";
PrincipalSearcher insPrincipalSearcher = new PrincipalSearcher();
insPrincipalSearcher.QueryFilter = insUserPrincipal;
PrincipalSearchResult<Principal> results = insPrincipalSearcher.FindAll();
foreach (Principal p in results)
{
Console.WriteLine(p.Name);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.