zoukankan      html  css  js  c++  java
  • 在ASP.NET MVC中使用MySQL【并使用membership】

            大多数情况下我们使用.NET或ASP.NET(包括MVC)程序时,我们会同时选择SQL Server 或者SQL Express (其他微软产品)做数据库。但是今天使用MVC已经完全没有必要这么做了,ASP.NET MVC的创建与ASP.NET相似,在.NET中一个核心组件ADO.NET允许我们连接各种不同的数据库,我们只需要一个连接器就可以了。在MYSQL中提供了几种不同的连接器供我们选择,官方广泛接受的MYSQL.NET Connector
     
              使用官方提供的包我们可以使用完整的功能,需要注意的是我们下载Connector后我们要如何配置web.config来连结我们的ASP.NET MVC程序已经如何使用ASP.NET Mumbership。首先我们先下载MySQL Connector/NET,下载地址:http://dev.mysql.com/downloads/connector/net/  下载好后,我们需要配置我们的web.config文件:
    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="DatabaseConnection" 
          connectionString="Server=localhost;Port=3306;Database=yourdbname;Uid=yourusername;Pwd=yourpassword;" 
          providerName="MySql.Data.MySqlClient" />
      </connectionStrings>
      <!-- ... -->
    </configuration>
            现在我们就使用DatabaseConnection做为我们的主数据库连接了,按照惯例(Entity Freamwork)我们需要把继承自DbContext的类命名为DatabaseConnection。现在Entity Freamwork就可和MySQL数据库一起工作了。那么我们要如何使用Mumbership呢? 我们需要在web.config中做如下的配置:
    <configuration>
        <!-- ... -->
        
        <system.web>
            <!-- ... -->
    
            <authentication mode="Forms">
              <forms loginUrl="~/Account/LogOn" timeout="2880" />
            </authentication>
    
            <roleManager enabled="true" defaultProvider="MySQLRoleProvider">
              <providers>
                <clear/>
                <add name="MySQLRoleProvider" autogenerateschema="true" 
                     type="MySql.Web.Security.MySQLRoleProvider, MySql.Web, Version=6.5.4.0, 
                           Culture=neutral, PublicKeyToken=c5687fc88969c44d"
                     connectionStringName="MarioDB" applicationName="/" />
              </providers>
            </roleManager>
    
            <membership defaultProvider="MySQLMembershipProvider">
              <providers>
                <clear />
                <add name="MySQLMembershipProvider" autogenerateschema="true" 
                     type="MySql.Web.Security.MySQLMembershipProvider, MySql.Web, 
                           Version=6.5.4.0, Culture=neutral, PublicKeyToken=c5687fc88969c44d"
                     connectionStringName="MarioDB" enablePasswordRetrieval="false" 
                     enablePasswordReset="true" requiresQuestionAndAnswer="false" 
                     requiresUniqueEmail="true" maxInvalidPasswordAttempts="5" 
                     minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" 
                     passwordAttemptWindow="10" applicationName="/" />
              </providers>
            </membership>
    
            <!-- ... --->
        </system.web>
    </configuration>
            这里有一个重要的属性需要注意,如果我们没有设置autogenerateschema 为true的话,我们就要确保我们数据库中已经有包含mumbership和rule的表。这个不是必要的,但是我们后面为什么又要移除他呢?一方面是因为Database User不应该有太多权限,当表以创建好我们就要移除; 另一方面,我们要做额外的检查是否存在这些表,根本没有必要,删除会节省一定的时间。
     
  • 相关阅读:
    路由和数据传递
    ASP.NET MVC3 自定义编辑模版
    最新Bootstrap手册
    ASP.NET MVC Bundles 用法和说明(打包javascript和css)
    MVC匿名类传值学习
    .net通用类型转换方法
    C#.net XML的序列化与反序列化
    The Connection Strings Reference
    ASP.NET MVC使用AuthenticationAttribute验证登录
    ASP.NET MVC Bundles 之学习笔记
  • 原文地址:https://www.cnblogs.com/aces/p/MVC_MYSQL.html
Copyright © 2011-2022 走看看