我在SharePoint交换上发布了此问题,但没有引起任何注意。任何帮助将不胜感激。
我已经实现了一个网站,并添加了一个名为“ SG_Uploader
”的SharePoint组。
在该组中,我ONLY
有一个名为“ AD_L6
”的Active Directory组,并且AD_L6中有许多用户。
如果用户来到站点并且我想检查他是否可以上传文档,我将使用以下代码,该代码非常简单:
SPWeb web = // ...
SPGroup group = web.SiteGroups["SG_Uploader"];
if (group.ContainsCurrentUser)
{
// allow user to upload
}
现在,我想做同样的事情,不是针对当前用户,而是针对拥有用户名的特定用户。通过这种方式,我想编写类似
的代码SPWeb web = // ...
SPGroup group = web.SiteGroups["SG_Uploader"];
if (group.ContainsUser(username))
{
// allow user to upload
}
我不知道我能做到这一点。请告知。
答案 0 :(得分:2)
以下代码供您参考。
var username = "user1";
var spGroupName = "SG_Uploader";
var adGroupName = "AD_L6";
using (SPSite spSite = new SPSite("http://sp2013/sites/team/"))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPUser user = spWeb.EnsureUser(adGroupName);
if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(spGroupName)))
{
var principalContext = new PrincipalContext(ContextType.Domain);
var group = GroupPrincipal.FindByIdentity(principalContext, adGroupName);
var isGroupMember = group.Members.Any(x => x.Name == username);
if (isGroupMember)
{
Console.WriteLine("User " + username + " is a member of group " + spGroupName);
}
else
{
Console.WriteLine("User " + username + " is not a member of group "+spGroupName);
}
}
}
}
答案 1 :(得分:0)
您可以做到。
string userName = "PERSEUS\\dmitry.kaloshin";
string groupName = "Home Members";
using (SPSite spSite = new SPSite("http://perseus"))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPUser user = spWeb.EnsureUser(userName);
if (user.Groups.Cast<SPGroup>().Any(g => g.Name.Equals(groupName)))
{
Console.WriteLine("User " + userName + " is a member of group " + groupName);
}
else
{
Console.WriteLine("User " + userName + " is NOT a member of group " + groupName);
}
}
}