InvalidProgramException 在 Microsoft.Office.Server.UserProfil

本文关键字:Server UserProfil Office Microsoft InvalidProgramException | 更新日期: 2023-09-27 18:35:03

我正在制作一个需要使用以下代码从活动目录中获取当前用户信息的 webpart:

protected void fetchUserInfo()
{
    System.Security.PermissionSet ps = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.Unrestricted);
    ps.Assert();
    Microsoft.SharePoint.SPServiceContext serviceContext = Microsoft.SharePoint.SPServiceContext.Current;
    UserProfileManager upm = new Microsoft.Office.Server.UserProfiles.UserProfileManager(serviceContext);
    ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;
    string userName = SPContext.Current.Web.CurrentUser.LoginName;
    UserProfile profile = upm.GetUserProfile(userName);
    foreach (ProfileSubtypeProperty prop in pspm.PropertiesWithSection)
    {
    }
}

但是,InvalidProgramException会引发在线ProfileSubtypePropertyManager pspm = upm.DefaultProfileSubtypeProperties;

错误消息是:

公共语言运行库检测到无效程序。

我试过谷歌搜索 Common Language Runtime detected an invalid program. sharepointUserProfileManager但没有太多信息。

可能是什么问题?


刚才看到异常在UserProfileManager上流动,所以SPServiceContext无效,当我在serviceContext上查看SiteSubscriptionId属性时,我发现它00000000-0000-0000-0000-000000000000

那么这意味着什么呢?是否有其他方法可以从 SharePoint 获取当前用户信息?

InvalidProgramException 在 Microsoft.Office.Server.UserProfil

最后,我改变了主意,使用UserPrincipal来获取用户信息:

    PrincipalContext _principalcontext = GetPrincipalContext();
    UserPrincipal _user = UserPrincipal.FindByIdentity(_principalcontext, IdentityType.SamAccountName, UID);
    if (_user != null)
    {
        DirectoryEntry directoryEntry = _user.GetUnderlyingObject() as DirectoryEntry;
        //Property is here;
    }