iT邦幫忙

0

c# WebSecurity 問題

我目前想在我的程式中加上權限,我使用c#的WebSecurity
我參考範例中的程式並自行實作
但是遇到一個問題
http://ithelp.ithome.com.tw/upload/images/20160808/20096630tB5MVBbkrT.png

看更多先前的討論...收起先前的討論...
fillano iT邦超人 1 級 ‧ 2016-08-09 14:49:29 檢舉
...你參考什麼範例程式?
fillano iT邦超人 1 級 ‧ 2016-08-09 15:03:49 檢舉
http://www.asp.net/web-pages/overview/security/16-adding-security-and-membership 嗎?
石頭 iT邦高手 1 級 ‧ 2016-08-09 16:04:15 檢舉
對!!
不好意思我忘記附上網址><,
我有在config中加,
<appSettings>
<add key="enableSimpleMembership" value="true" />
</appSettings>
fillano iT邦超人 1 級 ‧ 2016-08-09 17:20:31 檢舉
然後?你做了什麼?DB有設定嗎?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

1 個回答

0
fillano
iT邦超人 1 級 ‧ 2016-08-09 17:34:29
最佳解答

剛剛試寫一下,並不困難才對。

  1. appSettings中啟動simpleMembership,跟你的步驟一樣
  2. 從伺服器管理員=>資料連接加入一個localdb來做測試,假設名字叫做TestMembership
  3. 調整一下Web.config中的connectionStrings,確認這樣可以連上
  4. Global.asax.cs加上:
if (!WebMatrix.WebData.WebSecurity.Initialized)
{
    WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DefaultConnection", "TestUsers", "id", "name", true);
}
  1. Ctrl+F5啟動網站
  2. 從資料連結中打開DefaultConnection,應該會多出幾個資料表:TestUsers(id, name)及其他由WebSecurity及SimpleMembership自動產生的資料表
  3. 設計一個註冊頁面,透過
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(username, password);

來新增用戶
8. 透過WebSecurity.Login來驗證用戶登入

大致如上,只是簡單測試。

石頭 iT邦高手 1 級 ‧ 2016-08-09 22:31:47 檢舉

fillano上面的問題排解掉了^^
但我發現另一個問題..
因為我連資料表是我目前系統的資料表,
初始化如下
WebSecurity.InitializeDatabaseConnection
("LibraryConn", "Library_UserInfo", "Id", "Lib_username",autoCreateTables: true);
他目前可以對應到我的用戶名 但是密碼似乎對應不到
在使用此類時 是否要按照他預設tableschema來操作
不然會有問題?

fillano iT邦超人 1 級 ‧ 2016-08-10 14:24:26 檢舉

是阿XD

不然你參考一下原始碼:WebMatrix.WebData.SimpleMembershipProvider

然後寫一個自己的MembershipProvider,繼承SimpleMembershipProvider,改寫需要改寫的部份,來搭配你的資料庫。

石頭 iT邦高手 1 級 ‧ 2016-08-10 14:47:15 檢舉

好^^
瞭解了!!
感謝大大的解說

我要發表回答

立即登入回答