为WebApi项目增加用户注册功能

一、增加Filter目录:
   Mvc4项目下,Filter文件夹下的InitializeSimpleMembershipAttribute.cs,其作用是用来为数据库创建用户、角色管理表格的。
   移植到webapi项目,修改命名空间。
 
   涉及到2个问题:
   1、需要引用webmetrix.webdata程序集。在引用|添加引用|扩展,可以找到两个程序集,选择2.0版本。
   2、需要models里的UsersContext类的声明
 
   这意味着我们必须将mvc4的models里的AccountModels.cs文件迁移过来
 
  编译通过之后,我们在Filter中InitializeSimpleMembershipAttribute.cs里面
 public SimpleMembershipInitializer() 这个唯一的方法里,第一行设置断点,看看启动程序,会不会执行初始化工作。
 
  结果,没有在此断点处停下。查看数据库,也没有新增用户管理的相关表格。
  这里设置断点的目的是:
 1、确保每次修改,都能正常运行
 2、弄清初始化过程,是在首次程序启动的时候执行,还是在首次使用用户管理功能的时候执行。
 3、我们先指定我们的连接字符串,替代DefaultConnection
    WebSecurity.InitializeDatabaseConnection("WebLite", "UserProfile", "UserId", "UserName", autoCreateTables: true);
 
二、首先尝试 注册功能。
1、我们先将AccountController移植过来
    显然,我们只关心注册功能,因此,修改注释掉类里的全部内容,仅保留注册方法的get、post方法。注释掉引起编译问题的语句。
 
   编译一次,通过。
 
2、我们加入注册功能的View,同样移植过来
  修改,编译通过
 
3、好了,我们现在就看看,Account控制器的注册方法能否正常运行。
启动程序,我们看到浏览器的地址栏为:http://localhost:15946
修改为:
表示我们要执行AccountController的Register方法,注意这种访问是"get",该方法返回View(),表示按Mvc的约定返回同名的Register.cshtml视图。
 
4、很好,我们设在Filter中断点,其作用了:说明执行注册方法的时候,系统判断数据库中没有相应表格,准备自动创建表格。
选择继续,出现异常:

 
5、这个异常什么意思?
   首先,找不到连接字符串DefaultConnection,其次,这处异常出现在Web.config的45行,信息如下:
{"在应用程序配置中找不到连接名称“DefaultConnection”,或者连接字符串为空。 (E:\\WebLite\\WebLite\\web.config line 45)"}
 
再次“继续",页面上更为清晰:
行 43:     <membership defaultProvider="DefaultMembershipProvider">
行 44:       <providers>
行 45:         <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
行 46:       </providers>
行 47:     </membership>
 
 
注意这是WebApi为了使用传统的membership认证方式而预先准备的。
 
我们显然不会使用这种方式,那么注释掉。
 
重新调试,继续异常:
 
"The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588
"未启用角色管理器功能。"
 
继续:页面呈现的错误信息更明显
 
6、那么我们跟踪一下
   重新运行,到断点处,每次按F10执行一条。
  执行完if (!context.Database.Exists())的时候,问题出现了:
   我们鼠标点Database,发现其连接字符串为:
Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|DefaultConnection.mdf;Initial Catalog=DefaultConnection;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
 
问题出在var context = new UsersContext()这里,创建的Contex....使用了默认的连接字符串
 
转到userContext处,用”转到定义"
嗯:
    public class UsersContext : DbContext
    {
        public UsersContext()
            : base("DefaultConnection")
        {
        }
 
        public DbSet<UserProfile> UserProfiles { get; set; }
    }
 
7、问题继续出现:没有启用角色管理器
   单纯从这点来看,明显的.....我们会考虑什么? 配置文件中的 enable role?
   事实上,在配置文件中加入:
    <compilation debug="true" targetFramework="4.5">
      <assemblies>
        <add assembly="WebMatrix.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
        <add assembly="WebMatrix.WebData, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
      </assemblies>
    </compilation>
  强迫执行----会启用角色管理等等
 
  现在继续出现问题----那是页面的
 
8、简单的修改注册页面
将最下面一行注释掉
@*@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}*@
 
9、运行
 
可以正常的注册用户
原文地址:https://www.cnblogs.com/Leo_wl/p/2747476.html