C#最简单的登录Web服务



因为演示程序,所以有下面问题:
1。password是明码传输。

本文在  C#建立最简单的web服务,无需IIS  的基础上完毕。
详细步骤:
一。RequestProcessor添加变量mLogin,改写ParseRequestAndProcess函数。


 protected bool mLogin = false;
        public bool ParseRequestAndProcess(string[] RequestLines)//解析内容
        {       

            char[] sp = new Char[1] { ' ' };
            string[] strs = RequestLines[0].Split(sp);
         
            if (strs[0] == "GET")
            {
                Send(strs[1], 0, 0);
            }
            else if ("POST" == strs[0])
            {
                for (int j = 1; j < RequestLines.Length; j++)
                {
                    if (RequestLines[j].IndexOf('&') >= 0)
                    {
                        if( mLogin )
                        {
                            SendHeadrAndStr("已经登录。不能反复登录。

");
                        }
                        else
                        {
                            //分拆username和password
                            String sName = "";
                            String sPassWord = "";
                            string[] s = RequestLines[j].Split('&');
                            for (int k = 0; k < s.Length; k++)
                            {
                                string[] sItems = s[k].Split('=');
                                if (null == sItems || sItems.Length < 2)
                                    continue;
                                if ("username" == sItems[0])
                                    sName = sItems[1];
                                if ("password" == sItems[0])
                                    sPassWord = sItems[1];
                            }

                            //模拟登录
                            if ("a" == sName && "b" == sPassWord)
                            {
                                mLogin = true;
                                SendHeadrAndStr("登录成功。");
                            }
                            else
                            {
                                SendHeadrAndStr("username、password不匹配。");
                            }
                            
                        }
                    }
                }

            }
                
            return false;
        }

二,改写index.htm。


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>3</title>
<script>
function check()
{

    var username=document.getElementById("username");

    var pass=document.getElementById("password");
    if(username.value=="")
    {
        alert("请输入username");
        username.focus();
        return false;
    }
    if(pass.value=="")
    {
        alert("请输入password");
        pass.focus();
        return false;
    }
return true;    
}
</script>
</head>
<body>
<form name= "LoginForm" method="post" onsubmit="return check()">
<table width="350" bgcolor="#ccffcc" style="border-color" border="1">
<tr>
<td width="100">
username
</td>
<td>
<input type="text" name="username"  >
</td>
</tr>
<tr>
<td>
密 码
</td>
<td>
<input type="password" name="password" >
</td>
</tr>
<tr>
<td>

</td>
<td>
<input type="submit" value="登 录" >
</td>
</tr>
</table>
</form>
</body>
</html>