1.MODEL
两个文件一个user,一个与之对应的usermap。
关于nh的配置部分不做说明,用fluent配置就可以。 model'层一个user对象
namespace MODEL { public class User { public virtual int ID { get; set; } public virtual Guid MemberID { get; set; } public virtual string Name { get; set; } public virtual string Password { get; set; } } }
2.IDAL
两个文件,一个IUser,一个用于membership的IAspMembership文件
namespace IDAL { public interface IAspMembership { void CreateUser(MODEL.User user); bool ValidateUser(string username, string password); } }
namespace IDAL { public interface IUser { void SaveUpdate(MODEL.User User); } }
3.DAL
这层除了实现IDAL,还有个helper文件夹用于nh的session调用的。
4.BLL
两个文件,一个与user对应的blluser文件,一个与aspmembership对应的aspmembershipprovider
在aspmembershipprovider中,我们要继承membershipprovider这个类中我们需要的方法。这里我们只继承创建用户的createuser方法和用于验证的valideuser方法。
另外,我们在aspmembershipprovider中,组合idal.IAspmembership,用于调用自己写的一些方法。
public class AspMembershipProvider : MembershipProvider { private IDAL.IAspMembership imem; public IDAL.IAspMembership IMem { get { if (imem == null) { imem = new DAL.DALAspMembership(); } return imem; } set { imem = value; } } public override MembershipUser CreateUser(string username, string password, string email,
string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status) { IMem.CreateUser(new MODEL.User() { MemberID=Guid.NewGuid(), Name=username, Password=password }); MembershipUser mu = new MembershipUser("memProvider", username, providerUserKey, "", "", "", true, true, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now); status = MembershipCreateStatus.Success; return mu; } public override bool ValidateUser(string username, string password) { MODEL.User user=IMem.GetUser(username); return user.Password == password; } } }
5.网站
三个文件,default,login和web.config
default.cs
<body> <form id="form1" runat="server"> <div> <asp:LoginName ID="LoginName1" FormatString="Welcome {0}!" runat="server" /> <asp:LoginStatus ID="LoginStatus2" runat="server" /> </div> </form> </body>
login.cs
<body> <form id="form1" runat="server"> <hr /> <asp:Login ID="Login1" runat="server"> </asp:Login> <hr /> <div> <asp:CreateUserWizard ID="CreateUserWizard1" runat="server"> <WizardSteps> <asp:CreateUserWizardStep ID="CreateUserWizardStep1" runat="server"> </asp:CreateUserWizardStep> <asp:CompleteWizardStep ID="CompleteWizardStep1" runat="server"> </asp:CompleteWizardStep> </WizardSteps> </asp:CreateUserWizard> </div> </form> </body>
web.config
<configuration> <connectionStrings> <add name="sqlconn" connectionString="Server=.\sql2008;database=AspNHibernate;uid=sa;pwd=aa123" providerName="System.Data.SqlClient"/> </connectionStrings> <system.web> <authentication mode="Forms"/> <membership defaultProvider="memProvider"> <providers> <clear/> <add name="memProvider" type="BLL.AspMembershipProvider" connectionStringName="sqlconn" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/> </providers> </membership> <compilation debug="true" targetFramework="4.0" /> </system.web> </configuration>
这里不要忘记authentication,不加这段loginname会变成你的计算机名称。