Ada*_*kin 9 c# directoryservices active-directory
我在Web应用程序中使用UserPrincipal类的GetAuthorizationGroups方法时遇到问题.
使用以下代码,我收到"尝试检索授权组时,发生错误(5)"
PrincipalContext context = new PrincipalContext(ContextType.Domain, null, "DC=MyCompany,DC=COM", "username", "password");
UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "joe.blogs");
var groups = p.GetAuthorizationGroups();
Run Code Online (Sandbox Code Playgroud)
我相信这段代码在某种程度上起作用.
这是错误的堆栈跟踪.
[PrincipalOperationException: While trying to retrieve the authorization groups, an error (5) occurred.]
System.DirectoryServices.AccountManagement.AuthZSet..ctor(Byte[] userSid, NetCred credentials, ContextOptions contextOptions, String flatUserAuthority, StoreCtx userStoreCtx, Object userCtxBase) +317279
System.DirectoryServices.AccountManagement.ADStoreCtx.GetGroupsMemberOfAZ(Principal p) +441
System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroupsHelper() +78
System.DirectoryServices.AccountManagement.UserPrincipal.GetAuthorizationGroups() +11
Run Code Online (Sandbox Code Playgroud)
通过从PrincipalContext构造函数中删除用户名和密码详细信息并更改应用程序池(在iis7中)以同一用户(username@mycompany.com)运行 - 以下代码有效.
PrincipalContext context = new PrincipalContext(ContextType.Domain, null, "DC=MyCompany,DC=COM");
UserPrincipal p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "joe.blogs");
var groups = p.GetAuthorizationGroups();
Run Code Online (Sandbox Code Playgroud)
我需要让第一个示例中的代码工作 - 我不希望将应用程序池作为域用户运行,以使此代码正常工作.
我处理了同样的问题.见有关类似问题的讨论./sf/answers/584347221/
解决方案如下:
public List<GroupPrincipal> GetGroups(string userName)
{
var result = new List<GroupPrincipal>();
PrincipalContext ctx = GetContext(); /*function to get domain context*/
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, userName);
if (user != null)
{
PrincipalSearchResult<Principal> groups = user.GetAuthorizationGroups();
var iterGroup = groups.GetEnumerator();
using (iterGroup)
{
while (iterGroup.MoveNext())
{
try
{
Principal p = iterGroup.Current;
result.Add((GroupPrincipal) p);
}
catch (PrincipalOperationException)
{
continue;
}
}
}
}
return result;
}
Run Code Online (Sandbox Code Playgroud)
错误 5 表示ERROR_ACCESS_DENIED,这表明存在与权限相关的问题。也就是说,以下代码对我有用,在 Windows 7 上运行,网站作为默认应用程序池运行:
.aspx页面“body”的内容:
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
Run Code Online (Sandbox Code Playgroud)
隐藏代码:
protected void Page_Load(object sender, EventArgs e)
{
var Context = new PrincipalContext(ContextType.Domain, "logon_domain", "username", "password");
var principal = UserPrincipal.FindByIdentity(Context, "user_to_query");
var groups = principal.GetAuthorizationGroups();
GridView1.DataSource = groups;
GridView1.DataBind();
}
Run Code Online (Sandbox Code Playgroud)
在我的示例中,logon_domain
是 的左侧domain_name\username
,而不是您使用的域规范的样式。我的解决方案可能适合你,也可能不适合你。如果没有,则确实表明某处存在权限问题。
归档时间: |
|
查看次数: |
10667 次 |
最近记录: |