专题八的上篇大致讨论了MemberRole中的Membership实现,对于运用Membership进行web开发足够,但是对于想更深入了解Membership实现机理的朋友那是远远不够的,这个专题我们更深入一下了解Membership。
其实MemberRole是一个非常好的资源包,借住Reflector这个优秀的工具,你可以对其进行代码分析。它无论是在组建的构架、代码的设计、数据库表的建立、存储过程的使用等都是非常优秀的,你是程序员也好构架师也罢,其中可以学习的真的很多很多,我在整个分析的过程中也深深受益。
由于MemberRole中的Membership只实现了对SQL Server的操Provider类,即SqlMembershipProvider类。因此我们从SqlMembershipProvider开始分析。Provider模型在上篇已经做过介绍,SqlMembershipProvider类继承了MembershipProvider,并实现其所有的抽象方法。在分析之前先看两个类:MembershipUser与MembershipUserCollection。
MembershipUser,先看看代码:(代码中省略的具体实现,只有方法与属性名称)
public class MembershipUser
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// Methods
protected MembershipUser();
public MembershipUser(MembershipProvider provider, string name, object providerUserKey, string email, string passwordQuestion, string comment, bool isApproved, bool isLockedOut, DateTime creationDate, DateTime lastLoginDate, DateTime lastActivityDate, DateTime lastPasswordChangedDate, DateTime lastLockoutDate);
public virtual bool ChangePassword(string oldPassword, string newPassword);
public virtual bool ChangePasswordQuestionAndAnswer(string password, string newPasswordQuestion, string newPasswordAnswer);
public virtual string GetPassword();
public virtual string GetPassword(string passwordAnswer);
public virtual string ResetPassword();
public virtual string ResetPassword(string passwordAnswer);
public override string ToString();
public virtual bool UnlockUser();
internal virtual void Update();
private void UpdateSelf();
// Properties
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual string Comment { get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual DateTime CreationDate { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual string Email { get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual bool IsApproved { get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual bool IsLockedOut { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public bool IsOnline { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual DateTime LastActivityDate { get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual DateTime LastLockoutDate { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual DateTime LastLoginDate { get; set; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual DateTime LastPasswordChangedDate { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual string PasswordQuestion { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual MembershipProvider Provider { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual object ProviderUserKey { get; }
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
public virtual string UserName { get; }
// Fields
private string _Comment;
private DateTime _CreationDate;
private string _Email;
private bool _IsApproved;
private bool _IsLockedOut;
private DateTime _LastActivityDate;
private DateTime _LastLockoutDate;
private DateTime _LastLoginDate;
private DateTime _LastPasswordChangedDate;
private string _PasswordQuestion;
private MembershipProvider _Provider;
private object _ProviderUserKey;
private string _UserName;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这是一个实体类,表示一个由Membership创建的User,该类中有这个User的一些基本状态,如该User的UserName、Email等,还有一些方法,如ChangePassword()、ResetPassword()等(如果你是初学者,还在为建立一个对象需要什么属性,包含什么方法发愁,那这就是你应该好好学的,这也是OOP最基本的要求)。
MembershipUserCollection,这是一个MembershipUser类的容器,用来存放MembershipUser列表,记得上次广州.net俱乐部聚会时,我的演讲中有朋友在提出CS是否使用自定义类来存储用户列表,其实在这里可以看到CS中使用的就是自定义的类而不是DataSet(我想在asp.net 2.0正式发布后这也不会改变),这样做主要是因为考虑到性能与灵活性。
好了,回到SqlMembershipProvider类上来,我们具体分析一个有代表性质的方法:
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string text3;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
MembershipUser user1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!SecUtility.ValidateParameter(ref password, true, true, false, 0x80))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string text1 = base.GenerateSalt();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
string text2 = base.EncodePassword(password, (int) this._PasswordFormat, text1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (text2.Length > 0x80)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (passwordAnswer != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
passwordAnswer = passwordAnswer.Trim();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((passwordAnswer != null) && (passwordAnswer.Length > 0))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (passwordAnswer.Length > 0x80)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidAnswer;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
text3 = base.EncodePassword(passwordAnswer.ToLower(CultureInfo.InvariantCulture), (int) this._PasswordFormat, text1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
else
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
text3 = passwordAnswer;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!SecUtility.ValidateParameter(ref text3, this.RequiresQuestionAndAnswer, this.RequiresQuestionAndAnswer, false, 0x80))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidAnswer;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!SecUtility.ValidateParameter(ref username, true, true, true, 0x100))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidUserName;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!SecUtility.ValidateParameter(ref email, this.RequiresUniqueEmail, this.RequiresUniqueEmail, false, 0x100))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidEmail;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!SecUtility.ValidateParameter(ref passwordQuestion, this.RequiresQuestionAndAnswer, this.RequiresQuestionAndAnswer, false, 0x100))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidQuestion;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((providerUserKey != null) && !(providerUserKey is Guid))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidProviderUserKey;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (password.Length < this.MinRequiredPasswordLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int num1 = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
for (int num2 = 0; num2 < password.Length; num2++)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (!char.IsLetterOrDigit(password, num2))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
num1++;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (num1 < this.MinRequiredNonAlphanumericCharacters)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((this.PasswordStrengthRegularExpression.Length > 0) && !Regex.IsMatch(password, this.PasswordStrengthRegularExpression))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
ValidatePasswordEventArgs args1 = new ValidatePasswordEventArgs(username, password, true);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.OnValidatingPassword(args1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (args1.Cancel)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = MembershipCreateStatus.InvalidPassword;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlConnectionHolder holder1 = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
try
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
holder1 = SqlConnectionHelper.GetConnection(this._sqlConnectionString, true);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.CheckSchemaVersion(holder1.Connection);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlCommand command1 = new SqlCommand("dbo.aspnet_Membership_CreateUser", holder1.Connection);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.CommandTimeout = this.CommandTimeout;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.CommandType = CommandType.StoredProcedure;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@ApplicationName", SqlDbType.NVarChar, this.ApplicationName));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@UserName", SqlDbType.NVarChar, username));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@Password", SqlDbType.NVarChar, text2));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@PasswordSalt", SqlDbType.NVarChar, text1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@Email", SqlDbType.NVarChar, email));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@PasswordQuestion", SqlDbType.NVarChar, passwordQuestion));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@PasswordAnswer", SqlDbType.NVarChar, text3));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@IsApproved", SqlDbType.Bit, isApproved));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@UniqueEmail", SqlDbType.Int, this.RequiresUniqueEmail ? 1 : 0));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.CreateInputParam("@PasswordFormat", SqlDbType.Int, (int) this.PasswordFormat));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(this.GetTimeZoneAdjustmentParam());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
SqlParameter parameter1 = this.CreateInputParam("@UserId", SqlDbType.UniqueIdentifier, providerUserKey);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
parameter1.Direction = ParameterDirection.InputOutput;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(parameter1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
parameter1 = new SqlParameter("@ReturnValue", SqlDbType.Int);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
parameter1.Direction = ParameterDirection.ReturnValue;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
command1.Parameters.Add(parameter1);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
object obj1 = command1.ExecuteScalar();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
DateTime time1 = this.RoundToSeconds(DateTime.Now);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((obj1 != null) && (obj1 is DateTime))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
time1 = (DateTime) obj1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
int num3 = (parameter1.Value != null) ? ((int) parameter1.Value) : -1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if ((num3 < 0) || (num3 > 11))
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
num3 = 11;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
status = (MembershipCreateStatus) num3;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (num3 != 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
providerUserKey = new Guid(command1.Parameters["@UserId"].Value.ToString());
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return new MembershipUser(this, username, providerUserKey, email, passwordQuestion, null, isApproved, false, time1, time1, time1, time1, new DateTime(0x6da, 1, 1));
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
finally
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (holder1 != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
holder1.Close();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
holder1 = null;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
throw;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return user1;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
该方法实现建立一个用户的过程,建立后返回一个被建立的MembershipUser对象,如果建立失败MembershipUser对象为null(其实我早期做过一些项目的时候喜欢在建立对象成功后返回一个ID)。可以看到在这个方法中有很多的if语 句,它们是为了检验数据是否合法,这是必须的吗?其实不是,但对于构建一个强壮的底层代码这是必须的,不然一点点的错误都有可能导致系统的瘫痪。其实做项 目与做开发有的时候不太一样,企业的有些项目开发很多时候只要能实现功能就可以了,而且开发过程也集中在一些现有的代码或者组建的基础上,个人的错误不会 影响全局的运行,PM也不做过多要求。但如果是做产品,这个情况可能会有所改变,很多时候要求很严格,至少我是这样。在做完对输入参数的验证后,CreateUser建立与数据库的连接,这里是调用SqlConnectionHelper类下的GetConnection方法进行的,为了照顾初学者阅读,我这里这一下为什么需要把对数据库连接与操作写在SqlConnectionHelper类下,而不是直接采用SqlConnection提供的方法,其实这是一个设计模式的问题,Membership的实现需要很多的方法与数据库进行交换数据库,如果每次方法都调用一次SqlConnection的方法建立数据库连接,一来会造成大量的代码冗余,而且一旦数据库连接语句一旦改变,你就要去修改很多个方法,如果你把这个过程都包装在一个类下面,连接数据库就有统一的入口,一来容易维护,二来不会有太多的代码冗余,再者如果需要查找错误也非常容易。这里Membership采用的是存储过程,我们可以看到使用的是dbo.aspnet_Membership_CreateUser存储过程,好了,打开你的数据库,找到这个存储过程:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
PROCEDURE dbo.aspnet_Membership_CreateUser
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@ApplicationName NVARCHAR(256),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@UserName NVARCHAR(256),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@Password NVARCHAR(128),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordSalt NVARCHAR(128),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@Email NVARCHAR(256),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordQuestion NVARCHAR(256),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordAnswer NVARCHAR(128),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@IsApproved BIT,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@TimeZoneAdjustment INT,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@CreateDate DATETIME = NULL,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@UniqueEmail INT = 0,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordFormat INT = 0,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@UserId UNIQUEIDENTIFIER OUTPUT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
AS
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @ApplicationId UNIQUEIDENTIFIER
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @ApplicationId = NULL
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @NewUserId UNIQUEIDENTIFIER
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @NewUserId = NULL
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @IsLockedOut BIT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @IsLockedOut = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @LastLockoutDate DATETIME
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @LastLockoutDate = CONVERT( DATETIME, '17540101', 112 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @FailedPasswordAttemptCount INT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @FailedPasswordAttemptCount = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @FailedPasswordAttemptWindowStart DATETIME
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @FailedPasswordAttemptWindowStart = CONVERT( DATETIME, '17540101', 112 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @FailedPasswordAnswerAttemptCount INT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @FailedPasswordAnswerAttemptCount = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @FailedPasswordAnswerAttemptWindowStart DATETIME
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @FailedPasswordAnswerAttemptWindowStart = CONVERT( DATETIME, '17540101', 112 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @NewUserCreated BIT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @ReturnValue INT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ReturnValue = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @ErrorCode INT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
DECLARE @TranStarted BIT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @TranStarted = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @@TRANCOUNT = 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN TRANSACTION
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @TranStarted = 1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ELSE
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @TranStarted = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC dbo.aspnet_Applications_CreateApplication @ApplicationName, @ApplicationId OUTPUT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @@ERROR <> 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = -1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF (@CreateDate IS NULL)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC dbo.aspnet_GetUtcDate @TimeZoneAdjustment, @CreateDate OUTPUT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ELSE
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @CreateDate = DATEADD(n, -@TimeZoneAdjustment, @CreateDate) -- switch TO UTC time
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @NewUserId = UserId FROM dbo.aspnet_Users WHERE LOWER(@UserName) = LoweredUserName AND @ApplicationId = ApplicationId
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF ( @NewUserId IS NULL )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @NewUserId = @UserId
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
EXEC @ReturnValue = dbo.aspnet_Users_CreateUser @ApplicationId, @UserName, 0, @CreateDate, @NewUserId OUTPUT
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @NewUserCreated = 1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ELSE
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @NewUserCreated = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @NewUserId <> @UserId AND @UserId IS NOT NULL )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @@ERROR <> 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = -1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @ReturnValue = -1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF ( EXISTS ( SELECT UserId
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM dbo.aspnet_Membership
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE @NewUserId = UserId ) )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @UserId = @NewUserId
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF (@UniqueEmail = 1)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF (EXISTS (SELECT *
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FROM dbo.aspnet_Membership m WITH ( UPDLOCK, HOLDLOCK )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE ApplicationId = @ApplicationId AND LoweredEmail = LOWER(@Email)))
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = 7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
INSERT INTO dbo.aspnet_Membership
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
( ApplicationId,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UserId,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Password,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
PasswordSalt,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Email,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LoweredEmail,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
PasswordQuestion,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
PasswordAnswer,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
PasswordFormat,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IsApproved,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IsLockedOut,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
CreateDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LastLoginDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LastPasswordChangedDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LastLockoutDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FailedPasswordAttemptCount,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FailedPasswordAttemptWindowStart,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FailedPasswordAnswerAttemptCount,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
FailedPasswordAnswerAttemptWindowStart )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
VALUES ( @ApplicationId,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@UserId,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@Password,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordSalt,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@Email,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
LOWER(@Email),
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordQuestion,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordAnswer,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@PasswordFormat,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@IsApproved,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@IsLockedOut,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@CreateDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@CreateDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@CreateDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@LastLockoutDate,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@FailedPasswordAttemptCount,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@FailedPasswordAttemptWindowStart,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@FailedPasswordAnswerAttemptCount,
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
@FailedPasswordAnswerAttemptWindowStart )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @@ERROR <> 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = -1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF (@NewUserCreated = 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
UPDATE dbo.aspnet_Users
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET LastActivityDate = @CreateDate
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
WHERE @UserId = UserId
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @@ERROR <> 0 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @ErrorCode = -1
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GOTO Cleanup
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SELECT @CreateDate = DATEADD( n, @TimeZoneAdjustment, @CreateDate )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @TranStarted = 1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @TranStarted = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
COMMIT TRANSACTION
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
RETURN 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
Cleanup:
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
IF( @TranStarted = 1 )
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
BEGIN
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
SET @TranStarted = 0
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
ROLLBACK TRANSACTION
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
RETURN @ErrorCode
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
END
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
GO
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
够长的,不过没有关系,分几个部分看,首先是定义一些要发挥得参数,然后初始化,接着EXEC dbo.aspnet_Applications_CreateApplication,调用aspnet_Applications_CreateApplication存储过程,建立一个名字为@ApplicationName 的Application,如果该Application不存在的话.并且返回该Application的ID,这里的ApplicationName在web.config membership节点中设置过,即:dev。如果执行以上过程有错误,通过SQL的GOTO语句跳至Cleanup部分,执行ROLLBACK TRANSACTION,回滚这次操作。如果没有错误存储过程就接着向下执行,EXEC dbo.aspnet_GetUtcDate @TimeZoneAdjustment, @CreateDate OUTPUT,这是获得当前Utc时间。再下来就判断aspnet_Users表中用户的UserId是否在数据库中有该UserId(UserId是一个Guid),如果没有就在表aspnet_Users中建立。这时在进行一次失分发生错误的判断,执行的方法与前一次一样。再下来判断aspnet_Membership表中是否有该UserId存在,如果没有就根据@UniqueEmail参数判断是否允许Email在数据库中重复。最后才是把User的信息插入aspnet_Membership表,再下来还有一些对错误的处理...
其实这个存储过程并不复杂,但是非常繁琐的,也可以看出设计者对数据库检验的严格性的要求非常高。有了对存储过程一定的了解后,我们接下来那些传递的参数也就明白有何用处了,最后关闭数据库的连接,把返回的这些参数通过实例化一个MembershipUser类传递过去,然后返回这个实例化的MembershipUser,这样该方法就完成了一次操作。
最后我们看看数据库,Membership直接关联的有3个表
![](https://images.cnblogs.com/cnblogs_com/ugoer/membershipR1.JPG)
表很简单,关系也很明了,我就不多说了,总要给我留点时间吧,也给你自己留一些分析的空间,我要是全都说完了那你做什么?呵呵。
如果你了解CS系统,你肯定会提出这样一个疑问:用户信息不只表Membership中这一点呀,保存用户个性化设置的如选用什么语言、什么皮肤等等信息的数据都在哪里?期待吧,那是后面的Profile专题需要叙述的问题。