建立与现有成员mvc4 [英] Setting up existing membership with mvc4

查看:106
本文介绍了建立与现有成员mvc4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的SQL成员资格数据库,我与web表单中使用,我想将它与mvc4,但没有运气的工作,当我尝试获得由ID用户(我知道这个用户存在的话)我得到空异常。而网页我打开web应用程序的配置,我可以清楚地看到它没有任何成员或roles..etc。

下面是从我的web配置的一部分栌应用:

 <结构>
  <&是connectionStrings GT;
    <添加名称=ApplicationServices的connectionString =数据源= MYSERVER;初始目录= mydb的;用户ID =本身份识别码;密码= MYPWD的providerName =System.Data.SqlClient的/>
  < /&是connectionStrings GT;
  <&的System.Web GT;
    <编译调试=真targetFramework =4.0/>
    <身份验证模式=表格>
      <形式loginUrl =〜/帐号/的Login.aspx超时=2880/>
    < /认证>
    <会员和GT;
      <供应商>
        <清/>
        <添加名称=AspNetSqlMembershipProviderTYPE =System.Web.Security.SqlMembershipProvider的connectionStringName =ApplicationServicesenablePasswordRetrieval =假enablePasswordReset设置=真requiresQuestionAndAnswer =假requiresUniqueEmail =假maxInvalidPasswordAttempts =5minRequiredPasswordLength =6minRequiredNonalphanumericCharacters =0passwordAttemptWindow =10的applicationName =//>
      < /供应商>
    < /会员>
    <型材>
      <供应商>
        <清/>
        <添加名称=AspNetSqlProfileProviderTYPE =System.Web.Profile.SqlProfileProvider的connectionStringName =ApplicationServices的applicationName =//>
      < /供应商>
      <性状>
        <添加名称=UrgentPointsTYPE =System.Int32设置defaultValue =0/>
      < /性状>
    < / profile文件>
    < roleManager启用=真正的>
      <供应商>
        <清/>
        <添加的connectionStringName =ApplicationServices的applicationName =/NAME =AspNetSqlRoleProviderTYPE =System.Web.Security.SqlRoleProvider/>
        <添加的applicationName =/NAME =AspNetWindowsTokenRoleProviderTYPE =System.Web.Security.WindowsTokenRoleProvider/>
      < /供应商>
    < / roleManager>
....

和这里是MVC:

 <结构>
  <&是connectionStrings GT;
    <添加名称=DefaultConnection的connectionString =数据源= MYSERVER;初始目录= mydb的;用户ID =本身份识别码;密码= MYPWD的providerName =System.Data.SqlClient的/>
  < /&是connectionStrings GT;
  <&的appSettings GT;
    <添加键=网页:版本值=2.0.0.0/>
    <添加键=网页:启用VALUE =真/>
    <添加关键=preserveLoginUrlVALUE =真/>
    <添加键=ClientValidationEnabledVALUE =真/>
    <添加键=UnobtrusiveJavaScriptEnabledVALUE =真/>
  < /的appSettings>
  <&的System.Web GT;
    <编译调试=真targetFramework =4.0/>
    <身份验证模式=表格>
      <形式loginUrl =〜/帐号/登录超时=2880/>
    < /认证>
    <网页和GT;
      <&命名空间GT;
        <添加命名空间=System.Web.Helpers/>
        <添加命名空间=System.Web.Mvc/>
        <添加命名空间=System.Web.Mvc.Ajax/>
        <添加命名空间=System.Web.Mvc.Html/>
        <添加命名空间=System.Web.Routing/>
        <添加命名空间=System.Web.WebPages/>
      < /命名空间>
    < /页>
    <型材defaultProvider =DefaultProfileProvider>
      <供应商>
        <添加名称=DefaultProfileProviderTYPE =System.Web.Providers.DefaultProfileProvider,System.Web.Providers,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35的connectionStringName =DefaultConnection的applicationName =// >
      < /供应商>
    < / profile文件>
    <会员和GT;
      <供应商>
        <添加的connectionStringName =DefaultConnectionenablePasswordRetrieval =假
          enablePasswordReset设置=真requiresQuestionAndAnswer =假
          requiresUniqueEmail =假maxInvalidPasswordAttempts =5minRequiredPasswordLength =6
          minRequiredNonalphanumericCharacters =0passwordAttemptWindow =10
          的applicationName =/NAME =DefaultMembershipProviderTYPE =System.Web.Providers.DefaultMembershipProvider,System.Web.Providers,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35/>
      < /供应商>
    < /会员>
    < roleManager>
      <供应商>
        <添加的connectionStringName =DefaultConnection的applicationName =/
          NAME =DefaultRoleProviderTYPE =System.Web.Providers.DefaultRoleProvider,System.Web.Providers,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35/>
      < /供应商>
    < / roleManager>
    <的sessionState模式=是InProccustomProvider =DefaultSessionProvider>
      <供应商>
        <添加名称=DefaultSessionProviderTYPE =System.Web.Providers.DefaultSessionStateProvider,System.Web.Providers,版本= 1.0.0.0,文化=中性公钥= 31bf3856ad364e35的connectionStringName =DefaultConnection的applicationName =// >
      < /供应商>
    < /&的sessionState GT;
  < /system.web>
  < system.webServer>
    <验证validateIntegratedModeConfiguration =FALSE/>
    <模块runAllManagedModulesForAllRequests =真/>
  < /system.webServer>


解决方案

首先一定要切换到Asp.Net通用提供商(封装的NuGet),然后一定要连接到同一个数据库的旧应用程序。对于asp.net数据库的模式并没有改变,应该可以正常使用MVC 4。

I have an existing SQL membership db that I used with webforms, I am trying set it up to work with mvc4 but with no luck, when I try to get user by id(I know this user exists) I get null exception.And web I open web app config I can clearly see it has no members or roles..etc.

Here is part of config from my web froms app :

<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>
....

And here is for 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>

解决方案

First be sure to switch to Asp.Net Universal Providers (nuget package) and then be sure to connect to the same DB as the old application. The schema for the asp.net db hasn't changed and should work fine with MVC 4.

这篇关于建立与现有成员mvc4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆