??xml version="1.0" encoding="utf-8" standalone="yes"?>久久五月天婷婷,亚洲国产精品久久久久婷婷884 ,国产在线日本http://www.aygfsteel.com/vls/category/29413.html低调的美?/description>zh-cnSun, 11 May 2008 04:39:21 GMTSun, 11 May 2008 04:39:21 GMT60自定义MembershipProvider配合Asp.net 2.0 Login控ghttp://www.aygfsteel.com/vls/archive/2008/02/29/183019.htmlvlsvlsFri, 29 Feb 2008 10:51:00 GMThttp://www.aygfsteel.com/vls/archive/2008/02/29/183019.htmlhttp://www.aygfsteel.com/vls/comments/183019.htmlhttp://www.aygfsteel.com/vls/archive/2008/02/29/183019.html#Feedback0http://www.aygfsteel.com/vls/comments/commentRss/183019.htmlhttp://www.aygfsteel.com/vls/services/trackbacks/183019.html转自Q?a >http://www.programbbs.com/doc/2988.htm

转向Asp.net 2.0Q如果单单看Asp.net 2.0的例子和SDKQ相信你一定对pȝ自带的Login控g有比较深ȝ印象。Asp.Net 2.0的Login控g不用你写一行检用戯入是否合法的E序代码及相x询数据库的SQL脚本Q只需把相应的控g拖到Web表单中,卛_完成用户登陆Q创建用P用户角色理Q修改密码用戯l情况,取回密码{功能模块?br />   Login控g看上去近乎完,而我们现在手头正好来了一个项目要求采用Asp.net 2.0开发,而该目也要求有登陆Q用L理,权限理Q修改密码等功能Q相信绝大多Ch都会考虑使用Login控g来快速搞定这些要求。于是乎Q大家拿Z前的教学例子Q试着分析较深一层的代码Q看看该在哪里对Login控g修改一番,让它满手头目已设计好的数据库表结构。结果发玎ͼ除了aspx文g里面可以对Login控g的外观,提示文字可以自定义外Qcs文g中愣是找不到一行代码,然后l箋MSDN和GoogleQ终于知道,要在自己的项目中直接使用pȝ自带的Login控gQ需要做Q项修改工作Q?br />   1、根据你选用的数据库Q修改Web.config中相应的connectionStrings。系l默认的数据库是SQL Server 2005 ExpressQ如果我们的数据库是Access/SQL2000/2005/OracleQ当然要大改一番了?br />   2、改完Web.configq不够,我们q得执行C:\WINNT\Microsoft.NET\Framework\v2.0.507\aspnet_sqlreg.exe注册你的sql serverQ该E序的作用是在你的数据库中徏立Login控g需要的所有资?大约有上十个表,三十多条存储q程Q上十个视图{等)Q如果你使用的是access/orcaleQ或者是其他格式的数据库Q那你自己去Google相应的SQL脚本吧?br />   OKQ想到Login控g帮你节省的工作量Q相信不h都会咬着牙完成上面的Q项工作。完成上面2工作后Q大家接着读项目需求,发现有用L理和权限管理,q好开发资料上提到Login控g集成的Role角色理模块正好与之对应Q不q以后我们创Z个用户后Q还要再q入一个页面给用户选择所属用LQ当Ӟ采用Role的话Q我们可以设定一个用户同时属于多个用LQ貌似功能很强大哟。l读目需求,发现q些目的用户对象还有不Login控g中没有的属性要保存Q回头再ȝMSDNQ发现Profile可以帮我们解册个问题?br />   嗯,除开使用Login控gQ运行aspnet_sqlreg.exe帮我们徏立的上十个表Q三十多条存储过E,上十个视囄Q我们再不用保存用户的Q何信息了Q以后我们只用在Web.config文g和相应的cs代码中加上Role和Profile的处理代码,卛_完成该项目的登陆Q用L理,密码修改功能。算投入查MSDNQGoogle及修改Web.config文g和相应的cs代码的时_怿原来自己写过自定义Login控g的朋友已l准备发誓再也不Asp.net 2.0自带的Login控g了?br />   其实Q我们完全有更简z通用的办法来重用Asp.net 2.0自带的Login控gQ即只用它最基本的登陆及修改密码功能Q这Q个基本功能照旧?a >工具拖?a >控g出来往Web表单上一扔即可,一行代码都不多加。其他的用户/用户l管理,权限理不用扯上Login控gQ数据库想用什么品就用什么品,mysql/db2/infomax来者不?表结构想怎么设计怎么设计QE-R图,UML囄接照搬就?用户/用户l管理和权限理模块x么规定怎么规定Q自兌Q无限分U都行。M一句话Q让Login控g附带的上十个表,三十多条存储q程Q上十个视图见鬼d?br />   下面l说实现ҎQAsp.net 2.0的Login控g用到?个类来从数据库中获取相应的数据,分别是MemberShipproviderQRoleProvider及ProfileProviderQ系l自带的q?个类的方法的代码被隐藏v来了Q尽没公开Q但实际上就是用我上面一直念叨的上十个表Q三十多条存储过E,上十个视图。不你用什么数据库Q只要想使用Login控g的所有功能,必须保证该数据库中有与之对应的十来个表,三十多条存储q程Q十来个视图?br />   当然QMS的架构设计师也不是某些h惌中的那么无能Q上面的q三个类其实都是抽象c,pȝ的Login控g实际调用的是从这Q个cL生出来的针对SQL Server2000/2005的数据操作类Q灵zȝ架构设计正是在这里体现出来。既然MemberShipProviderQRoleProvider及ProfileProvider三大头是抽象c,那么我们完全可以自定义一个只针对用户表的username及passwordQ个列操作的MemberShipproviderzcd来,重写登陆验证Q修改密码以及其调用的方法,然后在Web.config中把membership的提供者指定ؓ我们自己写的MemberShipproviderzc,q样我们可以和原来一P把Login控g的登陆和修改密码Q个?a >控g往Web表单上一拖了事?br />   下面开始脓代码Q懒的深I的朋友们可以直接把我给出的cs代码贴回去,Zcs文g攑ֈApp_Code目录下,然后按照后面的Web.config修改相应的connectionStrings和membership卛_Q以后Q何项目要利用Asp.net 2.0的Login控g的登陆和修改密码都是q样照葫芦画瓢,够傻瓜吧?br />

  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节这样写QconnectionStrings和数据库有关Q不同的数据库差别很大,大家自己GoogleQ我׃列出来了?br />
<membership defaultProvider="MyMemberShip">
            
<providers>
                
<clear/>
                
<add name="MyMemberShip" type="MyMemberShip" requiresQuestionAndAnswer="false" connectionString="AdminSqlServer" minRequiredNonalphanumericCharacters="0" />
            
</providers>
        
</membership>

如果我们惛_用户验证登陆成功后做一些额外的处理Q可以给登陆控g的登陆按钮添加一个事Ӟ相应的代码如下:
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    
{
        
if (Membership.ValidateUser(Login1.UserName, Login1.Password))
        
{
            
//q里d你的额外处理代码Q如Session,login日至{等
            e.Authenticated = true;
        }

    }



vls 2008-02-29 18:51 发表评论
]]>
firefox下insertRowhttp://www.aygfsteel.com/vls/archive/2008/02/12/179652.htmlvlsvlsMon, 11 Feb 2008 18:14:00 GMThttp://www.aygfsteel.com/vls/archive/2008/02/12/179652.htmlhttp://www.aygfsteel.com/vls/comments/179652.htmlhttp://www.aygfsteel.com/vls/archive/2008/02/12/179652.html#Feedback0http://www.aygfsteel.com/vls/comments/commentRss/179652.htmlhttp://www.aygfsteel.com/vls/services/trackbacks/179652.html ie6  firefox opera insertRow 支持, 默认参数-1,默认d到最?/td> 支持Q?nbsp;但不支持默认参数 支持Q默认参?
默认d到最?br />
appendChild 不支?/td> 支持Q但是增?nbsp;tr后不影响rows 支持Q效果同insertRow(-1),影响rows dom level 1:http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-64060425

//向tableq加一个空行:
var
 otr = otable.insertRow(-1);
var otd = document.createElement("td");
otd.innerHTML 
= " "
otd.className 
= "XXXX"
otr.appendChild(otd); 

q样可以运行在q三U浏览器上了


vls 2008-02-12 02:14 发表评论
]]>
վ֩ģ壺 | | | ű| | | Ļ| | ٺ| | Ϫ| | | | | °Ͷ| | ƽ| ߰| ұ| ˫Ѽɽ| ÷ӿ| Ϫ| ʯ| | | Ϻ| Ӳ| | | | | | ̩˳| ȫ| Ұ| ƽ| | | ԣ| |