扩展 Active Directory 成员资格提供程序

发布于 12-11 08:25 字数 706 浏览 1 评论 0原文

我有一个 ASP.NET 网站,它将使用 Active Directory 来存储用户。 要求允许用户使用他们的电子邮件作为用户名。 Active Directory 不允许用户名中出现“@”等字符。 我创建了一个类来扩展 ActiveDirectoryMembershipProvider;它在调用之前将用户名从 ([email protected] 转换为 user_x0040_domain.com )基类函数。 示例:

  public override bool ValidateUser(string username, string password)
        {
            string encodedUsername = this.Encode(username);
            return base.ValidateUser(encodedUsername, password);
        }

问题是 MembershipUser 中不允许更改用户名。 我如何处理重写返回 MembershipUser 的方法? 像 MembershipUser GetUser(string username, bool userIsOnline)

I have an ASP.NET web site that will use Active Directory to store Users.
There is a requirement to allow users to use their emails as username.
Active directory will not allow characters like "@" in the usernames.
I created a class to extend the ActiveDirectoryMembershipProvider; It converts usernames from ([email protected] to user_x0040_domain.com ) before calling the base class functions.
example:

  public override bool ValidateUser(string username, string password)
        {
            string encodedUsername = this.Encode(username);
            return base.ValidateUser(encodedUsername, password);
        }

The Problem is that in the MembershipUser does not allow changing the username.
How can I handle overriding the methods that return MembershipUser?
Like MembershipUser GetUser(string username, bool userIsOnline)

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

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

发布评论

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

评论(1

白衬杉格子梦2024-12-18 08:25:25

我想您可以覆盖 Active Directory 提供程序返回的 MembershipUser,如下所示:

public class MyActiveDirectoryMembershipProvider : ActiveDirectoryMembershipProvider
{
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        ActiveDirectoryMembershipUser user = (ActiveDirectoryMembershipUser)base.GetUser(providerUserKey, userIsOnline);
        if (user == null)
            return null;

        return new MyActiveDirectoryMembershipUser(user);
    }

    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        MembershipUserCollection newColl = new MembershipUserCollection();
        foreach (ActiveDirectoryMembershipUser user in base.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords))
        {
            newColl.Add(new MyActiveDirectoryMembershipUser(user));
        }
        return newColl;
    }

    // TODO: check other methods to override
}

public class MyActiveDirectoryMembershipUser : ActiveDirectoryMembershipUser
{
    private string _userName;

    public override string UserName
    {
        get
        {
            return _userName;
        }
    }

    public MyActiveDirectoryMembershipUser(ActiveDirectoryMembershipUser user)
    {
        // TODO: do your decoding stuff here
        _userName = MyDecode(user.Email);
    }
}

注意:您需要确保覆盖所有返回用户的方法。它还对集合方法有一些性能影响,因为您需要复制集合(如我在示例中所示)。

I suppose you could do this overriding the MembershipUser returned by the Active Directory provider, something like this:

public class MyActiveDirectoryMembershipProvider : ActiveDirectoryMembershipProvider
{
    public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
    {
        ActiveDirectoryMembershipUser user = (ActiveDirectoryMembershipUser)base.GetUser(providerUserKey, userIsOnline);
        if (user == null)
            return null;

        return new MyActiveDirectoryMembershipUser(user);
    }

    public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
    {
        MembershipUserCollection newColl = new MembershipUserCollection();
        foreach (ActiveDirectoryMembershipUser user in base.FindUsersByName(usernameToMatch, pageIndex, pageSize, out totalRecords))
        {
            newColl.Add(new MyActiveDirectoryMembershipUser(user));
        }
        return newColl;
    }

    // TODO: check other methods to override
}

public class MyActiveDirectoryMembershipUser : ActiveDirectoryMembershipUser
{
    private string _userName;

    public override string UserName
    {
        get
        {
            return _userName;
        }
    }

    public MyActiveDirectoryMembershipUser(ActiveDirectoryMembershipUser user)
    {
        // TODO: do your decoding stuff here
        _userName = MyDecode(user.Email);
    }
}

NOTE: you will need to ensure all methods that return a user are overriden. It also has a some performance impact on collection methods, because you'll need to duplicate the collection (as I have shown in the sample).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文