C# 如何在Sharepoint中查找已登录的用户?

C# 如何在Sharepoint中查找已登录的用户?,c#,sharepoint,web-parts,C#,Sharepoint,Web Parts,我开发了一个必须部署在Sharepoint服务器上的web部件。我需要在web部件中登录sharepoint服务器的用户的用户名 如何获得该用户名?以下内容适用于我: SPWeb theSite = SPControl.GetContextWeb(Context); SPUser theUser = theSite.CurrentUser; string strUserName = theUser.LoginName; 然后签出。您可以使用: SPWeb web = SPControl.Get

我开发了一个必须部署在Sharepoint服务器上的web部件。我需要在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;


您还应该检查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//不要忘记添加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);
    }

SPContext.Current.Web.CurrentUser

您还可以通过_spPageContextInfo属性获取当前记录的用户ID

 _spPageContextInfo.userId

您将通过_spPageContextInfo获取当前用户的ID。试试这个可能会对你有所帮助。

或者更好:string strUserName=SPContext.Current.Web.CurrentUser.LoginName;我更喜欢第二种选择。对我来说,Catch是通过内部映射到AD的,它有一堆垃圾处理器,比如0.w | domain\username。但除此之外,第二个实现对我来说非常有用。@GoldBishop,您可以使用以下代码从声明表示中获取用户名:spclairprovidermanger mgr=spclairprovidermanger.Local;如果经理!=null{userName=mgr.DecodeClaimSPContext.Current.Web.CurrentUser.LoginName.Value;}有趣的是,我将通过一些测试运行它,看看它是如何解释的。但除此之外,我只做了userid=this.Context.User.Identity.Name;userid=userid.Substring userid.IndexOf'|'+1;并且能够将垃圾从我需要的信息中分离出来。这是可行的,但它不是垃圾。这是一个声明标识符,欢迎来到身份的新世界:取决于你如何看待它,我需要它的垃圾信息。我意识到信息是描述性的,但它是我不需要的开销文本;
 _spPageContextInfo.userId