隨筆-6  評論-2  文章-0  trackbacks-0

          轉自:http://www.programbbs.com/doc/2988.htm

          轉向Asp.net 2.0,如果單單看Asp.net 2.0的例子和SDK,相信你一定對系統自帶的Login控件有比較深刻的印象。Asp.Net 2.0的Login控件不用你寫一行檢測用戶輸入是否合法的程序代碼及相關查詢數據庫的SQL腳本,只需把相應的控件拖到Web表單中,即可完成用戶登陸,創建用戶,用戶角色管理,修改密碼用戶詳細情況,取回密碼等功能模塊。
            Login控件看上去近乎完美,而我們現在手頭正好來了一個項目要求采用Asp.net 2.0開發,而該項目也要求有登陸,用戶管理,權限管理,修改密碼等功能,相信絕大多數人都會考慮使用Login控件來快速搞定這些要求。于是乎,大家拿出以前的教學例子,試著分析較深一層的代碼,看看該在哪里對Login控件修改一番,讓它滿足手頭項目已設計好的數據庫表結構。結果發現,除了aspx文件里面可以對Login控件的外觀,提示文字可以自定義外,cs文件中愣是找不到一行代碼,然后繼續翻MSDN和Google,終于知道,要在自己的項目中直接使用系統自帶的Login控件,需要做2項修改工作:
            1、根據你選用的數據庫,修改Web.config中相應的connectionStrings。系統默認的數據庫是SQL Server 2005 Express,如果我們的數據庫是Access/SQL2000/2005/Oracle,當然要大改一番了。
            2、改完Web.config還不夠,我們還得執行C:\WINNT\Microsoft.NET\Framework\v2.0.507\aspnet_sqlreg.exe注冊你的sql server,該程序的作用是在你的數據庫中建立Login控件需要的所有資源(大約有上十個表,三十多條存儲過程,上十個視圖等等),如果你使用的是access/orcale,或者是其他格式的數據庫,那你自己去Google相應的SQL腳本吧。
            OK,想到Login控件幫你節省的工作量,相信不少人都會咬著牙完成上面的2項工作。完成上面2項工作后,大家接著讀項目需求,發現有用戶組管理和權限管理,幸好開發資料上提到Login控件集成的Role角色管理模塊正好與之對應,不過以后我們創建一個用戶后,還要再進入一個頁面給用戶選擇所屬用戶組,當然,采用Role的話,我們可以設定一個用戶同時屬于多個用戶組,貌似功能很強大喲。繼續讀項目需求,發現這些項目的用戶對象還有不少Login控件中沒有的屬性要保存,回頭再去翻MSDN,發現Profile可以幫我們解決這個問題。
            嗯,除開使用Login控件,運行aspnet_sqlreg.exe幫我們建立的上十個表,三十多條存儲過程,上十個視圖等,我們再不用建表保存用戶的任何信息了,以后我們只用在Web.config文件和相應的cs代碼中加上Role和Profile的處理代碼,即可完成該項目的登陸,用戶管理,密碼修改功能。算算投入查MSDN,Google及修改Web.config文件和相應的cs代碼的時間,相信原來自己寫過自定義Login控件的朋友已經準備發誓再也不碰Asp.net 2.0自帶的Login控件了。
            其實,我們完全有更簡潔通用的辦法來重用Asp.net 2.0自帶的Login控件,即只用它最基本的登陸及修改密碼功能,這2個基本功能照舊從工具箱拖個控件出來往Web表單上一扔即可,一行代碼都不多加。其他的用戶/用戶組管理,權限管理不用扯上Login控件,數據庫想用什么產品就用什么產品,mysql/db2/infomax來者不拒;表結構想怎么設計就怎么設計,E-R圖,UML圖直接照搬就成;用戶/用戶組管理和權限管理模塊想怎么規定就怎么規定,自關聯,無限分級都行。總之一句話:讓Login控件附帶的上十個表,三十多條存儲過程,上十個視圖見鬼去吧。
            下面細說實現方法,Asp.net 2.0的Login控件用到了3個類來從數據庫中獲取相應的數據,分別是MemberShipprovider,RoleProvider及ProfileProvider,系統自帶的這3個類的方法的代碼被隱藏起來了,盡管沒公開,但實際上就是使用我上面一直念叨的上十個表,三十多條存儲過程,上十個視圖。不管你用什么數據庫,只要想使用Login控件的所有功能,必須保證該數據庫中有與之對應的十來個表,三十多條存儲過程,十來個視圖。
            當然,MS的架構設計師也不是某些人想象中的那么無能,上面的這三個類其實都是抽象類,系統的Login控件實際調用的是從這3個類派生出來的針對SQL Server2000/2005的數據操作類,靈活的架構設計正是在這里體現出來。既然MemberShipProvider,RoleProvider及ProfileProvider三大頭是抽象類,那么我們完全可以自定義一個只針對用戶表的username及password2個列操作的MemberShipprovider派生類出來,重寫登陸驗證,修改密碼以及其調用的方法,然后在Web.config中把membership的提供者指定為我們自己寫的MemberShipprovider派生類,這樣我們就可以和原來一樣,把Login控件的登陸和修改密碼2個子控件往Web表單上一拖了事。
            下面開始貼代碼,懶的深究的朋友們可以直接把我給出的cs代碼貼回去,建個cs文件放到App_Code目錄下,然后按照后面的Web.config修改相應的connectionStrings和membership即可,以后任何項目要利用Asp.net 2.0的Login控件的登陸和修改密碼都是這樣照葫蘆畫瓢,夠傻瓜吧。

          1. using System;   
          2. using System.Data;   
          3. using System.Configuration;   
          4. using System.Data.SqlClient;   
          5. using System.Collections.Generic;   
          6. using System.Text.RegularExpressions;   
          7. using System.Data.SqlTypes;   
          8. using System.Web;   
          9. using System.Web.Security;   
          10.   
          11. /**//**//**//// <summary>   
          12. /// MyMemberShip 的摘要說明   
          13. /// </summary>   
          14. public class MyMemberShip : MembershipProvider   
          15. {   
          16.     private bool _requiresQuestionAndAnswer;   
          17.     private int _minRequiredPasswordLength;    
          18.     public MyMemberShip()   
          19.     {   
          20.         //   
          21.         // TODO: 在此處添加構造函數邏輯   
          22.         //   
          23.     }   
          24.     public override string ApplicationName   
          25.     {   
          26.         get  
          27.         {   
          28.             throw new Exception("The method or operation is not implemented.");   
          29.         }   
          30.         set  
          31.         {   
          32.             throw new Exception("The method or operation is not implemented.");   
          33.         }   
          34.     }   
          35.     public override void Initialize(string name, System.Collections.Specialized.NameValueCollection config)   
          36.     {   
          37.   
          38.         if (config["requiresQuestionAndAnswer"].ToLower() == "true")   
          39.             _requiresQuestionAndAnswer = true;   
          40.         else  
          41.             _requiresQuestionAndAnswer = false;   
          42.         int.TryParse(config["minPasswordLength"], out _minRequiredPasswordLength);   
          43.         base.Initialize(name, config);   
          44.     }   
          45.     public override bool ChangePassword(string username, string oldPassword, string newPassword)   
          46.     {   
          47.         using (SqlConnection connection = new SqlConnection(DBBase.DBConnectionString))   
          48.         {   
          49.             SqlCommand command = new SqlCommand();   
          50.             command.CommandText = "update [User] set user_pwd=@newpwd where user_name=@name and user_pwd=@oldpwd";   
          51.             command.Parameters.AddWithValue("@name", username);   
          52.             command.Parameters.AddWithValue("@oldpwd", CryptUtil.GetStringHashValue1(StringUtil.SqlEscape(oldPassword)));   
          53.             command.Parameters.AddWithValue("@newpwd", CryptUtil.GetStringHashValue1(StringUtil.SqlEscape(newPassword)));   
          54.             command.Connection = connection;   
          55.             connection.Open();   
          56.             return (int)command.ExecuteNonQuery() > 0 ? true : false;   
          57.         }   
          58.         //throw new Exception("The method or operation is not implemented.");   
          59.     }   
          60.   
          61.     public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)   
          62.     {   
          63.         throw new Exception("The method or operation is not implemented.");   
          64.     }   
          65.   
          66.     public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)   
          67.     {   
          68.         throw new Exception("The method or operation is not implemented.");   
          69.     }   
          70.   
          71.     public override bool DeleteUser(string username, bool deleteAllRelatedData)   
          72.     {   
          73.         throw new Exception("The method or operation is not implemented.");   
          74.     }   
          75.   
          76.     public override bool EnablePasswordReset   
          77.     {   
          78.         get { throw new Exception("The method or operation is not implemented."); }   
          79.     }   
          80.   
          81.     public override bool EnablePasswordRetrieval   
          82.     {   
          83.         get { throw new Exception("The method or operation is not implemented."); }   
          84.     }   
          85.   
          86.     public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)   
          87.     {   
          88.         throw new Exception("The method or operation is not implemented.");   
          89.     }   
          90.   
          91.     public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)   
          92.     {   
          93.         throw new Exception("The method or operation is not implemented.");   
          94.     }   
          95.   
          96.     public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)   
          97.     {   
          98.         throw new Exception("The method or operation is not implemented.");   
          99.     }   
          100.   
          101.     public override int GetNumberOfUsersOnline()   
          102.     {   
          103.         throw new Exception("The method or operation is not implemented.");   
          104.     }   
          105.   
          106.     public override string GetPassword(string username, string answer)   
          107.     {   
          108.         throw new Exception("The method or operation is not implemented.");   
          109.     }   
          110.   
          111.     public override MembershipUser GetUser(string username, bool userIsOnline)   
          112.     {   
          113.         DateTime myDate = DateTime.Today;   
          114.         MembershipUser user = new MembershipUser(   
          115.         Name, // Provider name   
          116.         username, // Username   
          117.         null// providerUserKey   
          118.         bobcy@21cn.com, // Email   
          119.         String.Empty, // passwordQuestion   
          120.         String.Empty, // Comment   
          121.         true// isApproved   
          122.         false// isLockedOut   
          123.         DateTime.Now, // creationDate   
          124.         DateTime.Now, // lastLoginDate   
          125.         DateTime.Now, // lastActivityDate   
          126.         DateTime.Now, // lastPasswordChangedDate   
          127.         new DateTime(1980, 1, 1) // lastLockoutDate   
          128.         );   
          129.         return user;    
          130.     }   
          131.   
          132.     public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)   
          133.     {   
          134.         throw new Exception("The method or operation is not implemented.");   
          135.     }   
          136.   
          137.     public override string GetUserNameByEmail(string email)   
          138.     {   
          139.         throw new Exception("The method or operation is not implemented.");   
          140.     }   
          141.   
          142.     public override int MaxInvalidPasswordAttempts   
          143.     {   
          144.         get { throw new Exception("The method or operation is not implemented."); }   
          145.     }   
          146.   
          147.     public override int MinRequiredNonAlphanumericCharacters   
          148.     {   
          149.         get { return 0; }   
          150.     }   
          151.   
          152.     public override int MinRequiredPasswordLength   
          153.     {   
          154.         get { return _minRequiredPasswordLength; }    
          155.     }   
          156.   
          157.     public override int PasswordAttemptWindow   
          158.     {   
          159.         get { throw new Exception("The method or operation is not implemented."); }   
          160.     }   
          161.   
          162.     public override MembershipPasswordFormat PasswordFormat   
          163.     {   
          164.         get { throw new Exception("The method or operation is not implemented."); }   
          165.     }   
          166.   
          167.     public override string PasswordStrengthRegularExpression   
          168.     {   
          169.         get { throw new Exception("The method or operation is not implemented."); }   
          170.     }   
          171.   
          172.     public override bool RequiresQuestionAndAnswer   
          173.     {   
          174.         get { return _requiresQuestionAndAnswer; }   
          175.     }   
          176.   
          177.     public override bool RequiresUniqueEmail   
          178.     {   
          179.         get { throw new Exception("The method or operation is not implemented."); }   
          180.     }   
          181.   
          182.     public override string ResetPassword(string username, string answer)   
          183.     {   
          184.         throw new Exception("The method or operation is not implemented.");   
          185.     }   
          186.   
          187.     public override bool UnlockUser(string userName)   
          188.     {   
          189.         throw new Exception("The method or operation is not implemented.");   
          190.     }   
          191.   
          192.     public override void UpdateUser(MembershipUser user)   
          193.     {   
          194.         throw new Exception("The method or operation is not implemented.");   
          195.     }   
          196.   
          197.     public override bool ValidateUser(string username, string password)   
          198.     {   
          199.         using (SqlConnection connection = new SqlConnection(DBBase.DBConnectionString))   
          200.         {   
          201.             SqlCommand command = new SqlCommand();   
          202.             command.CommandText = "select count(0) from [User] where user_name=@name and user_pwd=@pwd";   
          203.             command.Parameters.AddWithValue("@name", username);   
          204.             command.Parameters.AddWithValue("@pwd", password);   
          205.             command.Connection = connection;   
          206.             connection.Open();   
          207.             return ((int)command.ExecuteScalar()) > 0 ? true : false;   
          208.         }   
          209.     }   
          210. }  

          Web.Config的membership節這樣寫,connectionStrings和數據庫有關,不同的數據庫差別很大,大家自己Google,我就不列出來了。
          <membership defaultProvider="MyMemberShip">
                      
          <providers>
                          
          <clear/>
                          
          <add name="MyMemberShip" type="MyMemberShip" requiresQuestionAndAnswer="false" connectionString="AdminSqlServer" minRequiredNonalphanumericCharacters="0" />
                      
          </providers>
                  
          </membership>

          如果我們想在用戶驗證登陸成功后做一些額外的處理,可以給登陸控件的登陸按鈕添加一個事件,相應的代碼如下:
          protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
              
          {
                  
          if (Membership.ValidateUser(Login1.UserName, Login1.Password))
                  
          {
                      
          //這里添加你的額外處理代碼,如Session,login日至等等
                      e.Authenticated = true;
                  }

              }

          posted on 2008-02-29 18:51 vls 閱讀(1265) 評論(0)  編輯  收藏 所屬分類: ASP.net

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 黑河市| 天台县| 神木县| 安庆市| 大厂| 外汇| 申扎县| 南丰县| 孝感市| 郁南县| 河间市| 新昌县| 大冶市| 梓潼县| 会宁县| 本溪| 沈丘县| 鄄城县| 荣成市| 青浦区| 丰镇市| 张掖市| 中西区| 当涂县| 白河县| 莒南县| 宣威市| 衡阳市| 木里| 罗江县| 基隆市| 微山县| 双桥区| 伊通| 苏尼特左旗| 阿鲁科尔沁旗| 祁东县| 日土县| 曲周县| 延寿县| 长兴县|