我是 asp.net 的新手,我有一个问题。在我的 asp.net web 目录中,我有一些用户页面和一个管理员文件夹,我想在我的 web.config 文件中使用一些标签,如身份验证和授权来确定我的管理员用户名和密码,并确定只有管理员可以使用页面在管理文件夹中,但我的问题是我没有在我的网络中使用任何登录控件让我的用户登录。我使用一些文本框来提供用户信息,然后我连接到我在数据库中的表并检查他们的信息,我想知道在这种情况下我该怎么做?如果有人帮助我,我会变得非常感激。用户使用他们的电子邮件和密码登录,这是我的代码:
<tr>
<td>
Email :
</td>
<td class="style1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
</td>
<td class="style1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ErrorMessage="please enter your email!"
ControlToValidate="TextBox1"
Display="Dynamic" ForeColor="#CC0000" ValidationGroup="SignIn"></asp:RequiredFieldValidator>
<br />
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ErrorMessage="incorrect format!" ValidationExpression="\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" Display="Dynamic" ForeColor="#CC0000" ControlToValidate="TextBox1" ValidationGroup="SignIn"></asp:RegularExpressionValidator>
<br />
</td>
</tr>
<tr>
<td>
password:
</td>
<td class="style1">
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password" ></asp:TextBox>
<br />
</td>
</tr>
<tr>
<td>
</td>
<td class="style1">
<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="Enter your password" ControlToValidate="TextBox2" Display="Dynamic" ForeColor="#CC0000" ValidationGroup="SignIn" ></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td>
</td>
<td class="style1">
<asp:Button ID="Button2" runat="server" Text="enter"
Width="123px" onclick="Button2_Click" ValidationGroup="SignIn" />
</td>
我已经改变了它:
protected void Button2_click(object sender, EventArgs e)
{
if (Membership.ValidateUser(TextBox1.Text.Trim(), TextBox2.Text.Trim()))
{
if (Roles.IsUserInRole(TextBox1.Text.Trim(), "Admin"))
{
Response.Redirect("Admin/Default.aspx");
Session["user"] = "admin";
}
// ad.Text = "admin";
}
else
{
int c = -1;
SqlConnection cn2 = new SqlConnection();
cn2.ConnectionString = "server = . ; database = mobile_store ; Trusted_Connection=true";
DataTable tb = new DataTable();
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = cn2;
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "Check_Email_Pass";
cmd2.Parameters.AddWithValue("@mail", TextBox1.Text.Trim());
cmd2.Parameters.AddWithValue("@pass", TextBox2.Text.Trim());
cmd2.Parameters.Add("@res", SqlDbType.Int);
cmd2.Parameters["@res"].Direction = ParameterDirection.Output;
SqlDataAdapter da = new SqlDataAdapter(cmd2);
da.Fill(tb);
try
{
cn2.Open();
cmd2.ExecuteNonQuery();
c = Convert.ToInt32(cmd2.Parameters["@res"].Value);
switch (c)
{
case 1:
{
Session["user"] = tb.Rows[0][0].ToString() + " " + tb.Rows[0][1].ToString();
Session["authenticate"] = true;
Session["id"] = Convert.ToInt32( tb.Rows[0][2]);
((MasterPage)this.Master).lable2Visible = Session["user"].ToString();
Label2.Text = "Welcome" + " " + tb.Rows[0][0].ToString() + " " + tb.Rows[0][1].ToString();
TextBox1.Text = null;
if (Session["pagesource"] != null)
{
Response.Redirect((string)Session["pagesource"]);
}
else
{
Response.Redirect("~/user_page.aspx");
}
}
break;
case 0:
{
Label2.Text = "your password is wrong";
}
break;
case 2:
{
Label2.Text = "this email has registered before <br/> please sign up first";
TextBox3.Text = TextBox1.Text.Trim();
}
break;
default:
break;
}
}
}
catch (Exception ex)
{
Label1.Text = ex.ToString();
}
finally { cn2.Close(); }
}
在我的 web.config 文件中
<system.Web>
<authentication mode="Forms">
<forms timeout="20" slidingExpiration="true" cookieless="AutoDetect" protection="All" requireSSL="false" enableCrossAppRedirects="false" loginUrl="entrance_before_paying.aspx" >
<credentials passwordFormat="Clear">
<user name="elmiragolshanff@yahoo.com" password="elmira" />
</credentials>
</forms>
</authentication>
</system.web>
<location path="Admin">
<system.web>
<authorization>
<allow users="elmiragolshanff@yahoo.com" roles="Admin" />
</authorization>
</location>
</system.web>