ADAM 和 Azman 以及 ASP.Net 表单身份验证

发布于 2024-11-30 20:12:17 字数 1102 浏览 3 评论 0 原文

有谁能够使 ADAM/Azman 与 ASP.Net 表单身份验证一起使用吗?默认 ADAM 角色提供程序仅适用于 AD 域用户。我读过的每一篇文章都说您需要为其编写一个自定义角色提供程序。

我还到处发现了一些自定义角色提供程序代码,但没有什么具体的。如果有人可以分享为此所需的角色提供者,那就太好了。

到目前为止,我已关注以下文章:

  1. 自定义角色提供程序(不起作用)- http:// /www.codeproject.com/KB/aspnet/active_directory_roles.aspx
  2. 部分自定义角色提供程序代码 - http://blogs.msdn.com/b/azman/archive /2006/05/06/591230.aspx
  3. 再次部分自定义角色提供程序代码 - http://blog.avanadeadvisor.com/blogs/johanr/archive /2009/01/20/12373.aspx
  4. MS 文章设置 ADAM 并将其与 ASP.Net 结合使用的步骤(Windows 身份验证
  5. )开始使用 ADAM 进行身份验证(无角色) - http://www.alexthissen.nl/blogs/main/archive/2007/07/26/getting-started-with-adam-and-asp-net-2-0.aspx

Has anyone been able to make ADAM/Azman work with ASP.Net forms authentication. The default ADAM role provider works only with AD Domain users. And every single article I have read says that you need to write a custom role provider for it.

I have also found out bits and pieces of custom role provider code here and there, but nothing concrete. If someone can share the roleprovider needed for this, that will be great.

I have followed following articles so far :

  1. Custom Role provider (doesn't work) - http://www.codeproject.com/KB/aspnet/active_directory_roles.aspx
  2. Partial Custom Role provider code - http://blogs.msdn.com/b/azman/archive/2006/05/06/591230.aspx
  3. Partial Custom Role provider code again - http://blog.avanadeadvisor.com/blogs/johanr/archive/2009/01/20/12373.aspx
  4. MS Article steps to setup ADAM and use it with ASP.Net (windows auth)
  5. Getting started with ADAM for authentication (no roles) - http://www.alexthissen.nl/blogs/main/archive/2007/07/26/getting-started-with-adam-and-asp-net-2-0.aspx

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

分分钟 2024-12-07 20:12:17

我有一个被黑版本,我认真地说是被黑。我不需要修改应用程序中的角色,因此我只实现了 2 个方法。我必须发送用户名和密码才能查询目录。有一天,我想弄清楚如何使用 ActiveDirectoryMembershipProvider 的连接字符串,但我没有花很多时间在它上面,这会简化事情。

 public class ActiveDirectoryFormsRoleProvider : RoleProvider
{
    public string DomainController { get; set; }
    public string ConnectionLDAPSuffix { get; set; }
    public string ConnectionUserName { get; set; }
    public string ConnectionPassword { get; set; }
    public override string ApplicationName { get; set; }

    public override bool IsUserInRole(string username, string roleName)
    {
        var roles = GetRolesForUser(username);
        return roles.Contains(roleName);
    }

    public override string[] GetRolesForUser(string username)
    {
        var results = new List<string>();
        using (var context = new PrincipalContext(ContextType.Domain, DomainController,ConnectionLDAPSuffix,ConnectionUserName,ConnectionPassword))
        {
            try
            {
                var p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

                //looping twice because I was getting AppDomainUnloadedException on 50% of the first attempts
                for (var i = 0; i < 2; i++)
                {
                    try
                    {
                        var groups = p.GetAuthorizationGroups();
                        foreach (GroupPrincipal group in groups)
                        {
                            var name = group.SamAccountName;
                            if (!string.IsNullOrWhiteSpace(name))
                                results.Add(group.SamAccountName);
                        }
                        break;
                    }
                    catch (AppDomainUnloadedException)
                    {

                    }
                }
            }
            catch (Exception ex)
            {
                throw new ProviderException("Unable to query Active Directory.", ex);
            }
        }

        return results.ToArray();
    }

...

由于某种原因,在我的生产服务器上,我必须尝试两次 GetAuthorizationGroups(),因为 50% 的情况下,第一次尝试会因抛出 AppDomainUnloadedException 而失败。您也许可以删除该 for 循环。

这是我的 web.config 元素:

<roleManager enabled="true" defaultProvider="ActiveDirectoryFormsRoleProvider">
      <providers>
        <clear />
       <add name="ActiveDirectoryFormsRoleProvider"
          type="myapp.ActiveDirectoryFormsRoleProvider" 
          applicationName="myapp"
          DomainController="domaincontroller.testdomain.corp"
          ConnectionLDAPSuffix="DC=testdomain,DC=corp"
          ConnectionUsername="username"
          ConnectionPassword="password"
        />
      </providers>
    </roleManager>

I have a hacked version, and I seriously mean hacked. I don't need to modify roles in my app, so I only implemented 2 methods. I had to send a username and password to query the directory. Someday I'd like to figure out how to use the ActiveDirectoryMembershipProvider's connection string, but I did not spend a lot of time with it, that would simplify things.

 public class ActiveDirectoryFormsRoleProvider : RoleProvider
{
    public string DomainController { get; set; }
    public string ConnectionLDAPSuffix { get; set; }
    public string ConnectionUserName { get; set; }
    public string ConnectionPassword { get; set; }
    public override string ApplicationName { get; set; }

    public override bool IsUserInRole(string username, string roleName)
    {
        var roles = GetRolesForUser(username);
        return roles.Contains(roleName);
    }

    public override string[] GetRolesForUser(string username)
    {
        var results = new List<string>();
        using (var context = new PrincipalContext(ContextType.Domain, DomainController,ConnectionLDAPSuffix,ConnectionUserName,ConnectionPassword))
        {
            try
            {
                var p = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, username);

                //looping twice because I was getting AppDomainUnloadedException on 50% of the first attempts
                for (var i = 0; i < 2; i++)
                {
                    try
                    {
                        var groups = p.GetAuthorizationGroups();
                        foreach (GroupPrincipal group in groups)
                        {
                            var name = group.SamAccountName;
                            if (!string.IsNullOrWhiteSpace(name))
                                results.Add(group.SamAccountName);
                        }
                        break;
                    }
                    catch (AppDomainUnloadedException)
                    {

                    }
                }
            }
            catch (Exception ex)
            {
                throw new ProviderException("Unable to query Active Directory.", ex);
            }
        }

        return results.ToArray();
    }

...

For some reason on my production server, I have to make 2 attempts of GetAuthorizationGroups() because 50% of the time the first attempt failed by throwing AppDomainUnloadedException. You might be able to remove that for loop.

And here is my web.config element:

<roleManager enabled="true" defaultProvider="ActiveDirectoryFormsRoleProvider">
      <providers>
        <clear />
       <add name="ActiveDirectoryFormsRoleProvider"
          type="myapp.ActiveDirectoryFormsRoleProvider" 
          applicationName="myapp"
          DomainController="domaincontroller.testdomain.corp"
          ConnectionLDAPSuffix="DC=testdomain,DC=corp"
          ConnectionUsername="username"
          ConnectionPassword="password"
        />
      </providers>
    </roleManager>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文