在Asp.net中利用MySQL數(shù)據(jù)庫進(jìn)行驗證
以下的文章主要向大家描述的是在Asp.net中利用MySQL數(shù)據(jù)庫進(jìn)行驗證 ,一直想把Asp.net與MySQL數(shù)據(jù)庫結(jié)合起來,包括使用.net自帶的 providers、membership、roles等登錄驗證,因為MsSql太大了,個人使用沒有必要。
在網(wǎng)上Google了半天,沒有發(fā)現(xiàn)有價值的東西,大都是些如果讀取、寫入MySQL數(shù)據(jù)等,而且網(wǎng)上還有很多誤導(dǎo)信息,要通過一些開源的插件(類)來實現(xiàn)。其實,MySQL數(shù)據(jù)庫官方的 MySQL-connector-net 早在5.1版本就支持asp.net authorization 了,可以在官方網(wǎng)站和google卻很少有配置教程,最終我在一土耳其網(wǎng)站上找到了詳細(xì)的配置說明,現(xiàn)整理如下:
1、下載 MySQL Connector,我下載的是5.2.5,地址:http://dev.MySQL.com/downloads/connector/net/5.2.html,要用安裝版本,這樣在安裝過程中會對machine.config 、vs2008 等進(jìn)行一些默認(rèn)配置,省去了手動配置的麻煩。
2、在網(wǎng)站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文件復(fù)制到網(wǎng)站bin文件夾并添加引用
4、在machine.config (c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Config\machine.config)中添加 autogenerateschema="true" ,自動生產(chǎn)相關(guān)表格結(jié)構(gòu),如下:
- <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 網(wǎng)站管理工具(項目 > ASP.NET 配置),在“提供程序”頁面選擇“為每項功能選擇不同的提供程序(高級)”,接下來在“成員資格提供程序”中選擇MySQLMembershipProvider ,在“角色提供程序”中選擇MySQLRoleProvider 。
6、接下來就是享受Asp.net 和 MySQL數(shù)據(jù)庫 帶來的樂趣吧!
測試過程中出現(xiàn)的以下錯誤再也不見了(英文版出錯信息,中文版出錯信息沒有保存):
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)
以上的相關(guān)內(nèi)容就是對在Asp.net中利用MySQL數(shù)據(jù)庫進(jìn)行驗證的介紹,望你能有所收獲。
【編輯推薦】