c# - 使用 mvc4 设置现有成员资格

标签 c# asp.net asp.net-mvc-4

我有一个与 webforms 一起使用的现有 SQL 成员数据库,我正在尝试将其设置为与 mvc4 一起使用但没有运气,当我尝试通过 id 获取用户时(我知道该用户存在)我得到空异常.而 web 我打开 web 应用程序配置我可以清楚地看到它没有成员或角色..等。

这是我的 web froms 应用程序的部分配置:

<configuration>
  <connectionStrings>
    <add name="ApplicationServices" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>
    <membership>
      <providers>
        <clear />
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
      </providers>
    </membership>
    <profile>
      <providers>
        <clear />
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
      </providers>
      <properties>
        <add name="UrgentPoints" type="System.Int32" defaultValue="0" />
      </properties>
    </profile>
    <roleManager enabled="true">
      <providers>
        <clear />
        <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
        <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
      </providers>
    </roleManager>
....

这里是 mvc 的:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=myserver;Initial Catalog=mydb;User ID=myid;Password=mypwd" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="2.0.0.0" />
    <add key="webpages:Enabled" value="true" />
    <add key="PreserveLoginUrl" value="true" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" />
    </authentication>
    <pages>
      <namespaces>
        <add namespace="System.Web.Helpers" />
        <add namespace="System.Web.Mvc" />
        <add namespace="System.Web.Mvc.Ajax" />
        <add namespace="System.Web.Mvc.Html" />
        <add namespace="System.Web.Routing" />
        <add namespace="System.Web.WebPages" />
      </namespaces>
    </pages>
    <profile defaultProvider="DefaultProfileProvider">
      <providers>
        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </profile>
    <membership>
      <providers>
        <add connectionStringName="DefaultConnection" enablePasswordRetrieval="false"
          enablePasswordReset="true" requiresQuestionAndAnswer="false"
          requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6"
          minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
          applicationName="/" name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </membership>
    <roleManager>
      <providers>
        <add connectionStringName="DefaultConnection" applicationName="/"
          name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
      </providers>
    </roleManager>
    <sessionState mode="InProc" customProvider="DefaultSessionProvider">
      <providers>
        <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />
      </providers>
    </sessionState>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <modules runAllManagedModulesForAllRequests="true" />
  </system.webServer>

最佳答案

这是我谷歌搜索 3 天后的方法。使用 MVC4 RC、.net Framework 4.5、SQL Server Express 和新 SqlMemberDB由 .NET 4 创建导致旧的 SqlMemberDB 缺少一些存储过程。

  1. <AppSetting> 中再添加一项设置如下禁用“简单成员(member)”:

    <add key="enableSimpleMembership" value="false" />
    
  2. 根据需要添加数据库连接。我使用的是 SQL Server 2000,因此禁用用户实例。

    <add name="ScqMember_Context" 
         connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=MemberDB;User ID=xxx;Password=xxx;Integrated Security=False;User Instance=False;MultipleActiveResultSets=False" 
         providerName="System.Data.SqlClient" />
    
  3. 从 v4 .NET 框架下的 machine.ini 复制设置配置。

    <membership>
        <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" 
                 type="System.Web.Security.SqlMembershipProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" 
                 connectionStringName="ScqMember_Context" 
                 enablePasswordRetrieval="false" enablePasswordReset="true" 
                 requiresQuestionAndAnswer="true" applicationName="/" 
                 requiresUniqueEmail="false" passwordFormat="Hashed" 
                 maxInvalidPasswordAttempts="5" minRequiredPasswordLength="7" 
                 minRequiredNonalphanumericCharacters="1" 
                 passwordAttemptWindow="10" passwordStrengthRegularExpression="" />
        </providers>
    </membership>
    <profile>
        <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider" 
                 connectionStringName="ScqMember_Context" 
                 applicationName="/" 
                 type="System.Web.Profile.SqlProfileProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </providers>
    </profile>
    <roleManager enabled="true">
        <providers>
            <clear />
            <add connectionStringName="ScqMember_Context" 
                 applicationName="/"
                 name="AspNetSqlRoleProvider" 
                 type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
        </providers>
    </roleManager>
    
  4. 如下更改帐户 Controller 的 Login()

     /// aspnetSQLMember Authentication
     if (ModelState.IsValid)
     {
         if (Membership.ValidateUser(model.UserName, model.Password))
         {
             FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
    
             if (Url.IsLocalUrl(returnUrl))
             {
                 return Redirect(returnUrl);
             }
             else
             {
                 return RedirectToAction("Index", "Home");
             }
         }
         else
         {
             ModelState.AddModelError("", "The user name or password provided is incorrect.");
         }
     }
    
  5. 如下从 AccountController 更新注销模块

    /// aspnetSQLMember Authentication
    FormsAuthentication.SignOut();
    

谢谢,

关于c# - 使用 mvc4 设置现有成员资格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9877318/

相关文章:

c# - 将 Entity Framework 模型转换为数据契约

c# - 从记事本元素中检索不同的值

javascript - WebApi 捕获连接超时错误

javascript - 如何将 Jquery Datatables Ellipsis 渲染器用于模板字段链接按钮?

c# - 具有合并单元格的 GridView

asp.net-mvc-4 - 使用 @Url.Content ("~"是否有好处)

c# - 检查用户是否在 asp.net mvc Identity 中扮演角色

c# - 带有ID列表的Elasticsearch多匹配过滤器

c# - 从字典中获取任何元素的最快方法

c# - 应该如何管理在函数范围内声明的定时器的清理?