C# 表单身份验证和Active Directory
目前,我有一些使用表单身份验证的项目,我可以让用户使用他们的广告帐户登录。我做这件事的方式很简单 web.configC# 表单身份验证和Active Directory,c#,asp.net,active-directory,forms-authentication,C#,Asp.net,Active Directory,Forms Authentication,目前,我有一些使用表单身份验证的项目,我可以让用户使用他们的广告帐户登录。我做这件事的方式很简单 web.config //first I set up the connection string to the active directory account <connectionStrings> <add name="ADService" connectionString="LDAP://ourDomainController/OU=stores,DC=DOMAIN
//first I set up the connection string to the active directory account
<connectionStrings>
<add name="ADService" connectionString="LDAP://ourDomainController/OU=stores,DC=DOMAIN,DC=net" />
<ConnectionStrings>
//Then I add the membership provider for active directory
<membership defaultProvider="AspNetActiveDirectoryMembershipProvider">
<providers>
<clear />
<!--Membership provider for Active Directory-->
<add name="AspNetActiveDirectoryMembershipProvider" type="System.Web.Security.ActiveDirectoryMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ADService" attributeMapUsername="sAMAccountName" />
</providers>
</membership>
我只是想知道是否有人知道一种正确的方法或者一种不同的方法。任何建议都是有帮助的!谢谢 广告角色提供者的理想方式您可以编写自己的角色提供者,链接“我的解决您的问题”我已经解决了这个问题。我使用了我在问题中提供的链接和Kamlesh提供的链接。问题是我的。。GetRolesForUser。我必须用这个代码来解决这个问题
public override string[] GetRolesForUser(string username)
{
List<string> allRoles = new List<string>();
var ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if (user != null)
{
var groups = user.GetGroups();
allRoles.AddRange(groups.Select(x => x.Name));
}
return allRoles.ToArray();
}
public重写字符串[]GetRolesForUser(字符串用户名)
{
List allRoles=new List();
var ctx=新的PrincipalContext(ContextType.Domain);
UserPrincipal user=UserPrincipal.FindByIdentity(ctx,用户名);
如果(用户!=null)
{
var groups=user.GetGroups();
AddRange(groups.Select(x=>x.Name));
}
返回allRoles.ToArray();
}
我不再获得NullReferenceException,这将查找当前使用的角色。确保您也在使用active directory中的组。希望这能拯救一些人 是的,我已经调查过了。上面提到var root的代码就是我得到null引用异常的代码。
public override string[] GetRolesForUser(string username)
{
List<string> allRoles = new List<string>();
var ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, username);
if (user != null)
{
var groups = user.GetGroups();
allRoles.AddRange(groups.Select(x => x.Name));
}
return allRoles.ToArray();
}