IT技术网www.itjs.cn

当前位置:首页 > 数据库 > MySQL > 在Asp.net中利用MySQL数据库进行验证

在Asp.net中利用MySQL数据库进行验证

发布时间:2010-06-02 12:32 来源:未知

以下的文章主要向大家描述的是在Asp.net中利用MySQL数据库进行验证 ,一直想把Asp.net与MySQL数据库结合起来,包括使用.net自带的 providers、membership、roles等登录验证,因为MsSql太大了,个人使用没有必要。

在网上Google了半天,没有发现有价值的东西,大都是些如果读取、写入MySQL数据等,而且网上还有很多误导信息,要通过一些开源的插件(类)来实现。其实,MySQL数据库官方的 MySQL-connector-net 早在5.1版本就支持asp.net authorization 了,可以在官方网站和google却很少有配置教程,最终我在一土耳其网站上找到了详细的配置说明,现整理如下:

1、下载 MySQL Connector,我下载的是5.2.5,地址:http://dev.MySQL.com/downloads/connector/net/5.2.html,要用安装版本,这样在安装过程中会对machine.config 、vs2008 等进行一些默认配置,省去了手动配置的麻烦。

2、在网站web.config文件上添加ConnectionString

<connectionStrings>   <remove name="LocalMySQLServer"/>   <add name="LocalMySQLServer" connectionString="server=localhost; 

user id=root; password=toor; persist security info=true; database=dorknozzle;

" providerName="MySQL.Data.MySQLClient"/>   </ connectionStrings> 

3、把MySQL.Data.dll文件复制到网站bin文件夹并添加引用

4、在machine.config (c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config)中添加 autogenerateschema="true" ,自动生产相关表格结构,如下:

<membership>   <providers>   <add name="MySQLMembershipProvider" autogenerateschema="true" type="MySQL.

Web.Security.MySQLMembershipProvider, MySQL.Web, Version=5.2.2.0, ... />   </providers>   </membership> 

5、在Vistual stuido 2008中打开Asp.net 网站管理工具(项目 > ASP.NET 配置),在“提供程序”页面选择“为每项功能选择不同的提供程序(高级)”,接下来在“成员资格提供程序”中选择MySQLMembershipProvider ,在“角色提供程序”中选择MySQLRoleProvider 。

6、接下来就是享受Asp.net 和 MySQL数据库 带来的乐趣吧!

测试过程中出现的以下错误再也不见了(英文版出错信息,中文版出错信息没有保存):

There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.

The following message may help in diagnosing the problem: Unable to initialize provider. Missing or incorrect schema.(c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config line 145)

上面说了这么多内容,是关于对在Asp.net中利用MySQL数据库进行验证的介绍,不知道各位对MySQL的认识是不是更上一层楼了,时时关注ITJS,学习最新Mysql技术。