您的位置:首页 > 编程语言 > C#

C#最简单的登录Web服务

2015-07-10 12:06 465 查看
由于演示程序,所以有以下问题:

1,密码是明码传输。

本文在 
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

                        {

                            //分拆用户名和密码

                            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("用户名、密码不匹配。");

                            }

                            

                        }

                    }

                }

            }

                

            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.focus();

        return false;

    }

    if(pass.value=="")

    {

        alert("请输入密码");

        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">

用户名

</td>

<td>

<input type="text" name="username"  id="username">

</td>

</tr>

<tr>

<td>

密 码

</td>

<td>

<input type="password" name="password" id="password">

</td>

</tr>

<tr>

<td>

</td>

<td>

<input type="submit" value="登 录" >

</td>

</tr>

</table>

</form>

</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: