I don't think so (at least, it gets through the first part of creating a user, and actually adds the new user to the underlying
SqlServer2005 datastore).
BTW, is there any way to attach to the ASPNET Configuration process so that I can "watch" it in the debugger?
Here's the source code:
Membership Provider
===============
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for membership
/// </summary>
public class OMLMembershipProvider : MembershipProvider
{
private membershipTableAdapters.bd_userTableAdapter ta;
public override void Initialize( string name, System.Collections.Specialized.NameValueCollection config )
{
base.Initialize(name, config);
ta = new membershipTableAdapters.bd_userTableAdapter();
string env = WebConfigurationManager.AppSettings["environment"];
ta.Connection.ConnectionString = WebConfigurationManager.ConnectionStrings[env].ConnectionString;
}
private string TransformPassword( string toTransform )
{
byte[] clearText = Encoding.UTF8.GetBytes(toTransform);
byte[] encryptedText = EncryptPassword(clearText);
return Convert.ToBase64String(encryptedText);
}
public override string ApplicationName
{
get
{
return "OMLMembershipProvider";
}
set
{
throw new Exception("The method or operation is not implemented.");
}
}
public override bool EnablePasswordReset
{
get { return true; }
}
public override bool EnablePasswordRetrieval
{
get { return false; }
}
public override bool RequiresQuestionAndAnswer
{
get { return false; }
}
public override int MaxInvalidPasswordAttempts
{
get { return Int32.MaxValue; }
}
public override int PasswordAttemptWindow
{
get { return 10; }
}
public override bool RequiresUniqueEmail
{
get { return true; }
}
public override MembershipPasswordFormat PasswordFormat
{
get { return MembershipPasswordFormat.Encrypted; }
}
public override int MinRequiredPasswordLength
{
get { return 1; }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { return 0; }
}
public override string PasswordStrengthRegularExpression
{
get { return String.Empty; }
}
public override bool ChangePassword( string username, string oldPassword, string newPassword )
{
membership.bd_userDataTable curDT = ta.GetDataByUserID(username.ToLower());
if( (curDT == null) || (curDT.Count != 1) ) return false;
if( curDT[0].password != TransformPassword(oldPassword.ToLower()) ) return false;
curDT[0].password = TransformPassword(newPassword.ToLower());
ta.Update(curDT[0]);
return true;
}
public override bool ChangePasswordQuestionAndAnswer( string username, string password, string newPasswordQuestion, string
newPasswordAnswer )
{
return true;
}
public override MembershipUser CreateUser( string username, string password, string email, string passwordQuestion, string
passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status )
{
OnValidatingPassword(new ValidatePasswordEventArgs(username, password, true));
if( ( username == null ) || ( username == String.Empty ) )
{
status = MembershipCreateStatus.InvalidUserName;
return null;
}
if( ( password == null ) || ( password == String.Empty ) )
{
status = MembershipCreateStatus.InvalidPassword;
return null;
}
if( ( email == null ) || ( email == String.Empty ) )
{
status = MembershipCreateStatus.InvalidEmail;
return null;
}
username = username.ToLower();
object numMatch = ta.NumMatchingUserID(username);
if( (int) numMatch != 0 )
{
status = MembershipCreateStatus.DuplicateUserName;
return null;
}
numMatch = ta.NumMatchingEmail(email.ToLower());
if( (int) numMatch != 0 )
{
status = MembershipCreateStatus.DuplicateEmail;
return null;
}
membership.bd_userDataTable curDT = ta.GetData();
membership.bd_userRow curRow = curDT.Newbd_userRow();
curRow.user_id = username;
curRow.password = TransformPassword(password.ToLower());
curRow.email = email.ToLower();
curRow.flags = 0;
curRow.moniker = String.Empty;
curDT.Addbd_userRow(curRow);
ta.Update(curRow);
MembershipUser retVal = new MembershipUser("OMLMembershipProvider", username, -9, email,
String.Empty, "Comment!", true, false, DateTime.Now, DateTime.MinValue, DateTime.MinValue,
DateTime.MinValue, DateTime.MaxValue);
status = MembershipCreateStatus.Success;
return retVal;
}
public override string GetPassword( string username, string answer )
{
throw new Exception("The method or operation is not implemented.");
}
public override string ResetPassword( string username, string answer )
{
throw new Exception("The method or operation is not implemented.");
}
public override void UpdateUser( MembershipUser user )
{
membership.bd_userDataTable curDT = ta.GetDataByUserID(user.UserName.ToLower());
if( (curDT == null) || (curDT.Count != 1) ) return;
curDT[0].email = user.Email;
ta.Update(curDT[0]);
}
public override bool ValidateUser( string username, string password )
{
membership.bd_userDataTable curDT = ta.GetDataByUserID(username.ToLower());
if( ( curDT == null ) || ( curDT.Count != 1 ) ) return false;
return ( curDT[0].password == TransformPassword(password) );
}
public override bool UnlockUser( string userName )
{
return true;
}
public override MembershipUser GetUser( string username, bool userIsOnline )
{
username = username.ToLower();
membership.bd_userDataTable curDT = ta.GetDataByUserID(username);
if( (curDT == null) || (curDT.Count != 1) ) return null;
MembershipUser retVal = new MembershipUser("OMLMembershipProvider", username, curDT[0].idnum,
curDT[0].email, String.Empty, "Comments!", true, false, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
return retVal;
}
public override MembershipUser GetUser( object providerUserKey, bool userIsOnline )
{
if( !( providerUserKey is int ) )
throw new ApplicationException("OMLMembershipProvider::GetUser() -- supplied providerUserKey is not an integer");
membership.bd_userDataTable curDT = ta.GetDataByUserIDNum((int) providerUserKey);
if( ( curDT == null ) || ( curDT.Count != 1 ) ) return null;
MembershipUser retVal = new MembershipUser("OMLMembershipProvider", curDT[0].user_id, curDT[0].idnum,
curDT[0].email, String.Empty, "Comments!", true, false, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue);
return retVal;
}
public override string GetUserNameByEmail( string email )
{
email = email.ToLower();
membership.bd_userDataTable curDT = ta.GetDataByEmail(email);
if( ( curDT == null ) || ( curDT.Count != 1 ) ) return null;
return curDT[0].user_id;
}
public override bool DeleteUser( string username, bool deleteAllRelatedData )
{
username = username.ToLower();
membership.bd_userDataTable curDT = ta.GetDataByUserID(username);
if( ( curDT == null ) || ( curDT.Count != 1 ) ) return false;
curDT[0].Delete();
ta.Update(curDT);
return true;
}
public override MembershipUserCollection GetAllUsers( int pageIndex, int pageSize, out int totalRecords )
{
membership.bd_userDataTable curDT = ta.GetData();
totalRecords = curDT.Count;
MembershipUserCollection retVal = new MembershipUserCollection();
for( int row = pageIndex * pageSize; row < ( pageIndex + 1 ) * pageSize; row++ )
{
if( row >= ( totalRecords - 1 ) ) break;
retVal.Add(new MembershipUser("OMLMembershipProvider", curDT[row].user_id, curDT[row].idnum,
curDT[row].email, String.Empty, "Comments!", true, false, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue));
}
return retVal;
}
public override int GetNumberOfUsersOnline()
{
return 1;
}
public override MembershipUserCollection FindUsersByEmail( string emailToMatch, int pageIndex, int pageSize, out int
totalRecords )
{
emailToMatch = emailToMatch.ToLower();
membership.bd_userDataTable curDT = ta.GetDataByEmailPattern(emailToMatch);
totalRecords = curDT.Count;
MembershipUserCollection retVal = new MembershipUserCollection();
for( int row = pageIndex * pageSize; row < ( pageIndex + 1 ) * pageSize; row++ )
{
if( row >= ( totalRecords - 1 ) ) break;
retVal.Add(new MembershipUser("OMLMembershipProvider", curDT[row].user_id, curDT[row].idnum,
curDT[row].email, String.Empty, "Comments!", true, false, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue));
}
return retVal;
}
public override MembershipUserCollection FindUsersByName( string usernameToMatch, int pageIndex, int pageSize, out int
totalRecords )
{
usernameToMatch = usernameToMatch.ToLower();
membership.bd_userDataTable curDT = ta.GetDataByUserIDPattern(usernameToMatch);
totalRecords = curDT.Count;
MembershipUserCollection retVal = new MembershipUserCollection();
for( int row = pageIndex * pageSize; row < ( pageIndex + 1 ) * pageSize; row++ )
{
if( row >= ( totalRecords - 1 ) ) break;
retVal.Add(new MembershipUser("OMLMembershipProvider", curDT[row].user_id, curDT[row].idnum,
curDT[row].email, String.Empty, "Comments!", true, false, DateTime.MinValue,
DateTime.MinValue, DateTime.MinValue, DateTime.MinValue, DateTime.MinValue));
}
return retVal;
}
}
RoleProvider
=========
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for roles
/// </summary>
public class OMLRoleProvider : RoleProvider
{
private membershipTableAdapters.bd_userTableAdapter ta;
public override void Initialize( string name, System.Collections.Specialized.NameValueCollection config )
{
base.Initialize(name, config);
ta = new membershipTableAdapters.bd_userTableAdapter();
string env = WebConfigurationManager.AppSettings["environment"];
ta.Connection.ConnectionString = WebConfigurationManager.ConnectionStrings[env].ConnectionString;
}
private RoleFlags TextToFlag( string flagName )
{
RoleFlags retVal = RoleFlags.None;
try
{
retVal = (RoleFlags) Enum.Parse(typeof(RoleFlags), flagName, true);
}
catch { }
return retVal;
}
private RoleFlags TextToFlag( string[] flagNames )
{
RoleFlags retVal = RoleFlags.None;
foreach( string curFlag in flagNames )
{
try
{
retVal |= (RoleFlags) Enum.Parse(typeof(RoleFlags), curFlag, true);
}
catch { }
}
return retVal;
}
public override string ApplicationName
{
get { return "OMLRoleProvider"; }
set
{
throw new Exception("The method or operation is not implemented.");
}
}
public override bool IsUserInRole( string username, string roleName )
{
RoleFlags toCheck = TextToFlag(roleName);
if( toCheck == RoleFlags.None ) return false;
object queryVal = ta.GetUserRoles(username.ToLower());
if( queryVal == System.DBNull.Value ) return false;
RoleFlags roleFlags = (RoleFlags) queryVal;
return ( ( roleFlags & toCheck ) == toCheck ) ;
}
public override string[] GetRolesForUser( string username )
{
object queryVal = ta.GetUserRoles(username.ToLower());
if( queryVal == System.DBNull.Value ) return new string[0];
RoleFlags roleFlags = (RoleFlags) queryVal;
RoleFlags[] allRoles = (RoleFlags[]) Enum.GetValues(typeof(RoleFlags));
List<string> roles = new List<string>();
foreach( RoleFlags curFlag in allRoles )
{
if( ( roleFlags & curFlag ) == curFlag )
roles.Add(Enum.GetName(typeof(RoleFlags), curFlag));
}
return roles.ToArray();
}
public override void CreateRole( string roleName )
{
throw new ApplicationException("You can't create additional roles with the OMLRoleProvider");
}
public override bool RoleExists( string roleName )
{
bool isOkay = false;
try
{
RoleFlags dummy = (RoleFlags) Enum.Parse(typeof(RoleFlags), roleName, true);
isOkay = true;
}
catch { }
return isOkay;
}
public override bool DeleteRole( string roleName, bool throwOnPopulatedRole )
{
throw new ApplicationException("You can't create delete roles with the OMLRoleProvider");
}
public override void AddUsersToRoles( string[] usernames, string[] roleNames )
{
RoleFlags toAdd = TextToFlag(roleNames);
membership.bd_userDataTable userDT = ta.GetData();
foreach( string curUser in usernames )
{
membership.bd_userRow[] userRows = (membership.bd_userRow[]) userDT.Select("user_id='" + curUser.ToLower() + "'");
if( userRows.Length != 1 ) continue;
userRows[0].flags |= (int) toAdd;
ta.Update(userRows[0]);
}
}
public override void RemoveUsersFromRoles( string[] usernames, string[] roleNames )
{
RoleFlags toRemove = TextToFlag(roleNames);
toRemove = ~toRemove;
membership.bd_userDataTable userDT = ta.GetData();
foreach( string curUser in usernames )
{
membership.bd_userRow[] userRows = (membership.bd_userRow[]) userDT.Select("user_id='" + curUser.ToLower() + "'");
if( userRows.Length != 1 ) continue;
userRows[0].flags &= (int) toRemove;
ta.Update(userRows[0]);
}
}
public override string[] GetUsersInRole( string roleName )
{
int toCheck = (int) TextToFlag(roleName);
membership.bd_userDataTable theDT = ta.GetData();
List<string> users = new List<string>();
foreach( membership.bd_userRow curRow in theDT )
{
if( ( curRow.flags & toCheck ) == toCheck )
users.Add(curRow.user_id);
}
return users.ToArray();
}
public override string[] GetAllRoles()
{
return Enum.GetNames(typeof(RoleFlags));
}
public override string[] FindUsersInRole( string roleName, string usernameToMatch )
{
int toCheck = (int) TextToFlag(roleName);
membership.bd_userDataTable curDT = ta.GetDataByUserIDPattern(usernameToMatch);
List<string> users = new List<string>();
foreach( membership.bd_userRow curRow in curDT )
{
if( ( curRow.flags & toCheck ) == toCheck )
users.Add(curRow.user_id);
}
return users.ToArray();
}
}
RoleFlags (used by RoleProvider)
========================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
[Flags]
public enum RoleFlags : int
{
None = 0,
SysAdmin = 1,
RegisteredUser = 2,
}
Web.config (I've redacted the keys for privacy reasons)
========
<membership defaultProvider="OMLMembershipProvider">
<providers>
<add name="OMLMembershipProvider"
type="OMLMembershipProvider" />
</providers>
/membership>
<roleManager enabled="true" defaultProvider="OMLRoleProvider">
<providers>
<add name="OMLRoleProvider"
type="OMLRoleProvider" />
</providers>
</roleManager>
<machineKey
decryption="AES"
validation="SHA1"
validationKey="redacted"
decryptionKey="redacted"
/>