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 (user@domain.com 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)
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).