asp.net第一篇

来源:互联网 发布:域名反向解析怎么设置 编辑:程序博客网 时间:2024/05/18 17:26

1、做登录页
.aspx文件中

<asp:textbox id="txtUserId" runat="server" text="DotNet" columns="15" maxlength="20" />
<asp:requiredfieldvalidator id="valUserId" runat="server" controltovalidate="txtUserId" errormessage="Please enter user ID." enableclientscript="False" />
                                
<asp:textbox id="txtPassword" runat="server" value="DotNet" columns="15" maxlength="20" textmode="Password" />
<asp:requiredfieldvalidator id="valPassword" runat="server" controltovalidate="txtPassword" errormessage="Please enter password." enableclientscript="False" />
            

 使用了errormessage而没有采用text
可以用于错误汇总,显示在validatorsummary控件中

enableclientscript="false"为禁止客户端验证
默认为true

private const string MSG_FAILURE = "Sign in failed! Please try again.";

if (Page.IsValid) {

string userId = WebComponents.CleanString.InputText(txtUserId.Text, 50);
string password = WebComponents.CleanString.InputText(txtPassword.Text, 50);

if (!accountController.ProcessLogin(userId, password)){
valUserId.ErrorMessage 
= MSG_FAILURE;
valUserId.IsValid 
= false;
}


}

        


Page.IsValid永远不会让你失望。。保证了验证

如果登录失败了,直接用valUserId.ErrorMessage显示错误信息,节省了在.aspx中添加一个Label控件来显示登录失败



并使用一个const string 的常量来放错误信息,,是一个聪明的方法

InputText()这个函数用来过滤用户的恶意输入

using System.Text;

public static string InputText(string inputString, int maxLength) {

            
            StringBuilder retVal 
= new StringBuilder();

            
// check incoming parameters for null or blank string
            if ((inputString != null&& (inputString != String.Empty)) {
                inputString 
= inputString.Trim();

                
//chop the string incase the client-side max length
                
//fields are bypassed to prevent buffer over-runs
                if (inputString.Length > maxLength)
                    inputString 
= inputString.Substring(0, maxLength);

                
//convert some harmful symbols incase the regular
                
//expression validators are changed
                for (int i = 0; i < inputString.Length; i++{
                    
switch (inputString[i]) {
                        
case '"':
                            retVal.Append(
"&quot;");
                            
break;
                        
case '<':
                            retVal.Append(
"&lt;");
                            
break;
                        
case '>':
                            retVal.Append(
"&gt;");
                            
break;
                        
default:
                            retVal.Append(inputString[i]);
                            
break;
                    }

                }


                
// Replace single quotes with white space
                retVal.Replace("'"" ");
            }


            
return retVal.ToString();
            
        }

或者

using System.Text.RegularExpressions;

public static string InputText(string text, int maxLength) {
            text 
= text.Trim();
            
if (string.IsNullOrEmpty(text))
                
return string.Empty;
            
if (text.Length > maxLength)
                text 
= text.Substring(0, maxLength);
            text 
= Regex.Replace(text, "[/s]{2,}"" ");    //two or more spaces
            text = Regex.Replace(text, "(<[b|B][r|R]/*>)+|(<[p|P](.|/n)*?>)"" ");    //<br>
            text = Regex.Replace(text, "(/s*&[n|N][b|B][s|S][p|P];/s*)+"" ");    //&nbsp;
            text = Regex.Replace(text, "<(.|/n)*?>"string.Empty);    //any other tags
            text = text.Replace("'""''");
            
return text;
        }



原创粉丝点击