【问题标题】:InvalidProgramException throws on Microsoft.Office.Server.UserProfiles.UserProfileManager在 Microsoft.Office.Server.UserProfiles.UserProfileManager 上引发 InvalidProgramException
【发布时间】:2016-05-28 22:19:03
【问题描述】:

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

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 检测到无效程序。

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

可能是什么问题?


刚刚看到UserProfileManager上出现异常,所以SPServiceContext无效,查看serviceContext上的SiteSubscriptionId属性发现是00000000-0000-0000-0000-000000000000

那是什么意思?有没有其他方法可以从 sharepoint 获取当前用户信息?

【问题讨论】:

  • 您是否尝试过调试? upm.DefaultProfileSubtypeProperties 是什么?另外,该服务是否存在,是否可以访问?
  • @JevgeniGeurtsen upm.DefaultProfileSubtypeProperties 抛出无效程序异常
  • 所以它不存在。 upm 实例是否有效?
  • @JevgeniGeurtsen 我检查了 upm 实例,发现它无效,请查看我的更新

标签: c# sharepoint-2013 web-parts


【解决方案1】:

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

    PrincipalContext _principalcontext = GetPrincipalContext();
    UserPrincipal _user = UserPrincipal.FindByIdentity(_principalcontext, IdentityType.SamAccountName, UID);

    if (_user != null)
    {
        DirectoryEntry directoryEntry = _user.GetUnderlyingObject() as DirectoryEntry;
        //Property is here;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-09
    • 2012-04-19
    • 2018-11-05
    • 1970-01-01
    • 1970-01-01
    • 2010-09-15
    • 1970-01-01
    相关资源
    最近更新 更多