寒假准备对我们团队开发的各个小系统进行整合和重写,由于这些系统需求分析并不够仔细,所以在后期缝缝补补,加上人员交替,使得代码结构凌乱不堪,让我这个有代码洁癖的人难受的要死,所以打算重新梳理,由于我们团队主攻asp.net mvc开发,(我对asp.net mvc的学习和使用是从2开始,到现在我们项目基本用的是3),重新写一遍系统,当然要有点新的尝试,虽然现在asp.net mvc5已发布,但感觉使用asp.net mvc4开发的人都不多,何况是5,而且出现问题估计网上搜索解决,也比较麻烦,所以还是一步一步来,用mvc4吧:
环境:
1、数据库:SQL Server 2008
2、开发:Visual Studio 2013/Framework4.0
开始构建基础框架
在VS2013中新建一个mvc4项目,新增加了些和以前不同的文件夹,删除掉自己不需要的,然后把以前项目的静态页面集成入项目,按结构把母板页,局部页设计完成,对照了些以前系统的各个模块,觉得登录,注册这块还是自己来完成搭建较好,看了下VS2013的mvc4项目的Account的东西,新增了SimpleMembership、WebMatrix等的一些东西,以前没接触过,看来mvc3到mvc4还是有点变化的(除了有打包功能等)
微软提供一套身份认证与授权的东西,但都是用微软自己的数据库字段等等,我们这种小作坊开发有点用不上,想用自己的数据库和字段结构,往往我们一般是将登录成功后的信息按情况存入session或是cookie中。
但我代码洁癖的性格不能容忍这样的写法,我一定要用微软的身份认证与授权的写法规范,来实现。
之前经过网上查资料,我们在asp.net mvc3项目中Account的写法如下:
所有的都在AccountModel中修改:
Services区域:在对IMembershipService的接口实例化方法时:重新申明自己定义的MembershipProvider(对MembershipProvider的方法的重写),结束。
但asp.net mvc4默认的AccountModel写法变了,新增了SimpleMembership,WebMatrix等,废话少说,经过MSDN查找资料和Stack Overflow看老外的介绍,再结合反编译WebMatrix的dll文件,大概看出:其实webMatrix就是对mvc3的account的封装,然后SimpleMembership就是自定义出SimpleMembership的数据库和自定义字段,那对于我们来说SimpleMembership是不需要的,我们要构建自己的MyMembership,然后该怎么做呢?
首先:webconfig中,禁用SimpleMembership
然后:告诉WebMatrix,我们要用的是哪个Membership?,即我们自己的MyMembership:Membership
最后:新建类,实现ExtendedMembershipProvider的方法
public class MyMembershipProvider : ExtendedMembershipProvider(引用using WebMatrix.WebData;)
{
public override bool ConfirmAccount(string accountConfirmationToken)
{
throw new NotImplementedException();
}
public override bool ConfirmAccount(string userName, string accountConfirmationToken)
{
throw new NotImplementedException();
}
public override string CreateAccount(string userName, string password, bool requireConfirmationToken)
{
throw new NotImplementedException();
}
public override string CreateUserAndAccount(string userName, string password, bool requireConfirmation, IDictionary
{
throw new NotImplementedException();
}
public override bool DeleteAccount(string userName)
{
throw new NotImplementedException();
}
public override string GeneratePasswordResetToken(string userName, int tokenExpirationInMinutesFromNow)
{
throw new NotImplementedException();
}
public override ICollection
{
throw new NotImplementedException();
}
public override DateTime GetCreateDate(string userName)
{
throw new NotImplementedException();
}
public override DateTime GetLastPasswordFailureDate(string userName)
{
throw new NotImplementedException();
}
public override DateTime GetPasswordChangedDate(string userName)
{
throw new NotImplementedException();
}
public override int GetPasswordFailuresSinceLastSuccess(string userName)
{
throw new NotImplementedException();
}
public override int GetUserIdFromPasswordResetToken(string token)
{
throw new NotImplementedException();
}
public override bool IsConfirmed(string userName)
{
throw new NotImplementedException();
}
public override bool ResetPasswordWithToken(string token, string newPassword)
{
throw new NotImplementedException();
}
public override string ApplicationName
{
get
{
throw new NotImplementedException();
}
set
{
throw new NotImplementedException();
}
}
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
throw new NotImplementedException();
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new NotImplementedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotImplementedException();
}
public override bool EnablePasswordReset
{
get { throw new NotImplementedException(); }
}
public override bool EnablePasswordRetrieval
{
get { throw new NotImplementedException(); }
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override int GetNumberOfUsersOnline()
{
throw new NotImplementedException();
}
public override string GetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new NotImplementedException();
}
public override string GetUserNameByEmail(string email)
{
throw new NotImplementedException();
}
public override int MaxInvalidPasswordAttempts
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredPasswordLength
{
get { throw new NotImplementedException(); }
}
public override int PasswordAttemptWindow
{
get { throw new NotImplementedException(); }
}
public override MembershipPasswordFormat PasswordFormat
{
get { throw new NotImplementedException(); }
}
public override string PasswordStrengthRegularExpression
{
get { throw new NotImplementedException(); }
}
public override bool RequiresQuestionAndAnswer
{
get { throw new NotImplementedException(); }
}
public override bool RequiresUniqueEmail
{
get { throw new NotImplementedException(); }
}
public override string ResetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override bool UnlockUser(string userName)
{
throw new NotImplementedException();
}
public override void UpdateUser(MembershipUser user)
{
throw new NotImplementedException();
}
public override bool ValidateUser(string username, string password)
{
throw new NotImplementedException();
}
}