==================webconfig==============
<authentication mode="Forms">
<forms loginUrl="login.aspx" />
</authentication>
<authorization>
<deny users="?"/>
</authorization>
===================CS======================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Data.SqlClient;
public partial class Default3 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string username = TextBox1.Text;
string pwd = TextBox2.Text;
bool yz = AuthenticateUser(username, pwd);
if (yz)
{
FormsAuthentication.RedirectFromLoginPage(username, false);
}
else
{
Response.Write("不存在用户");
}
}
private bool AuthenticateUser(string username,string pwd)
{
string connstring = System.Configuration.ConfigurationManager.ConnectionStrings["SampleDBConnectionString"].ConnectionString;
int n = 0;
using (SqlConnection conn = new SqlConnection(connstring))
{
SqlCommand cmd = new SqlCommand("select count(*) from users where username =@username and company =@age", conn);
cmd.Parameters.Add("@username",System.Data.SqlDbType.NVarChar,10).Value = username;
cmd.Parameters.Add("@age",System.Data.SqlDbType.NVarChar,20).Value = pwd;
conn.Open();
SqlDataReader rs = cmd.ExecuteReader();
while (rs.Read())
{
n = Convert.ToInt16 (rs.GetValue(0));
}
conn.Close();
}
return (n>0);
}
}
9250

被折叠的 条评论
为什么被折叠?



