【问题标题】:Fba roles with SharePoint user groups具有 SharePoint 用户组的 Fba 角色
【发布时间】:2009-03-20 11:39:20
【问题描述】:

我已经构建了自定义成员资格和角色提供程序。用户是属于公司的一些客户,我使用公司作为角色。

我想创建 SharePoint 组并向其中添加更多公司(例如行业类型),然后由 SPGroup 进行重定向和安全。

如何检索当前登录用户的 SPGroup?
我想在我的自定义登录页面中这样做,所以另一个问题是如何在知道登录名的情况下检索 SPUser 或 SPGroup ?

这就是我现在拥有的:


private List GetGroupsForUser(List roleAccounts)
{
    List groups = new List();
    SPSecurity.RunWithElevatedPrivileges(
     delegate()
     {
         using (SPSite site = new SPSite(SPContext.Current.Web.Site.ID))
         {
             SPUserCollection users = site.RootWeb.SiteUsers;
             foreach (string account in roleAccounts)
             {
                 SPGroupCollection accGroups = users[account].Groups;
                 foreach (SPGroup spg in groups)
                 {
                     groups.Add(spg);
                 }
             }
         }
     }
     );

    return groups;

}

private string GetRoleManagerName()
{
    foreach (KeyValuePair setting in SPContext.Current.Site.WebApplication.IisSettings)
    {
        if (string.IsNullOrEmpty(setting.Value.RoleManager) == false)
            return setting.Value.RoleManager.ToLower();
    }
    return null;
}

private List GetSpAccounts()
{
    List roleAccounts = new List();
    string roleProviderName = GetRoleManagerName();
    foreach (string role in Roles.GetRolesForUser(login.UserName))
    {
        roleAccounts.Add(roleProviderName + ":" + role.ToLower());
    }

    return roleAccounts;
}


// and now I can use it
List roleAccounts = GetSpAccounts();
List groups = GetGroupsForUser(roleAccounts);

但我有一种感觉,我不应该像这样手动执行此操作。如果只将角色添加到组中,目标受众将如何工作?

【问题讨论】:

    标签: sharepoint moss forms-authentication


    【解决方案1】:

    使用 SPUser 类的 OwnedGroups 属性返回用户拥有的组的集合。

    更新误解的问题:

    1. 获取当前登录用户:SPContext.Current.Web.CurrentUser
    2. 将该用途添加到组中:SPGroup.Users.Add([username],[email],[name],[notes]);

    更新,第三次魅力。因此,您想根据用户拥有的角色找出用户所在的组? 有点综合以上两种尝试来回答:

    var matched = from r in SPContext.Current.Web.CurrentUser.Roles
                  where r.Name = [Role]
                  select r.Groups;
    

    请注意,Roles 属性在下一版本的 SharePoint 中将不起作用! 我个人认为 SPContext.Current.Web.CurrentUser.Groups 将是一种更简单的方法来确定用户所在的组,但它忽略了您的角色要求。

    【讨论】:

    • 感谢您的回答,但 SPContext.Current.Web.CurrentUser.Roles 有问题。还有一个不是我在 OnLoggedIn 事件的登录页面上做所有这些。
    猜你喜欢
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2016-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多