web.config 验证登陆
web.config中: <authentication mode="Forms">
<forms loginUrl="login.aspx" timeout="2880" name="xx" />
</authentication>
<authorization>
<deny users="?"/> 添加此节后login.aspx页面 css格式全部清除,提交事件不响应......
</authorization>
login.aspx 页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="login.aspx.cs" Inherits="project.login" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.
<html xmlns="http://www.
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户登录</title>
<link href="images/login.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="javascript/jquery.min.js"></script>
<script type="text/javascript">
(function($){ $(document).ready(function(){
$('.captcha').focus(function () {
$('.yzm-box').show();
});
$('.captcha').focusout(function () {
$('.yzm-box').hide();
});
})(jQuery);
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<div id="message-box"> 用户名或密码错误! </div>
<div id="wrap">
<div id="header"> </div>
<div id="content-wrap">
<div class="space"> </div>
<div class="content">
<div class="field"><label>账 户:</label>
<input class="username" name="" type="text" id="username" runat="server"/></div>
<div class="field"><label>密 码:</label>
<input class="password" name="" type="password" id="password" runat="server"/><br /></div>
<div class="field"><label>验证码:</label>
<input class="captcha" maxlength="6" name="" type="text" runat="server" /><br />
<div class="yzm-box"> </div>
</div>
<div class="btn">
<%-- <input name="" type="submit" class="login-btn" value="" />--%>
<asp:Button ID="Button1" runat="server" Text=" " CssClass="login-btn"
onclick="Button1_Click" />
</div>
</div>
</div>
<div id="footer"> </div>
</div>
</div>
</form>
</body>
</html>
login.aspx cs :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace project
{
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
bool flag=checklogin(username.Value.Trim(), password.Value.Trim());
if (flag==true)
{
Session["uname"]=username.Value.Trim();
Response.Redirect("default.aspx");
}
else
{
ScriptManager.RegisterClientScriptBlock(this, this.GetType(), null, "<script>alert('用户名或密码错误!')</script>", false);
}
}
//验证用户名和密码
public static bool checklogin(string uname, string pwd)
{
int rs = 0;
using (SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["tangzheng"].ConnectionString))
{
string sql = "select count(*)from users where username=@username and userpwd=@userpwd ";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.Add("@username", SqlDbType.VarChar).Value = uname;
cmd.Parameters.Add("@userpwd", SqlDbType.VarChar).Value = pwd;
con.Open();
rs = Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
if (rs>0)
{
return true;
}
else
{
return false;
}
}
}
}
}
正常情况:
[attach]70785[/attach]
[ 本帖最后由 q314045513 于 2013-5-9 17:39 编辑 ]