Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: form authentication according microsoft site


Message #1 by "Evan" <raisorsharp@h...> on Tue, 29 Oct 2002 15:31:10
Hello everybody,

All day I'm TRYING to get the coding to work that is on the website of 
microsoft, but I guess I'm doing SOMETHING wrong, but I don't get it :(
Hopefully someone will be able to shine some light on this.

The *almost* step by step guide for Form Authentication with C# / ASP.net 
is on:
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q301240&LN=EN-US

I created the pages (well, just the web.config file and the login.aspx)
When I login, nothing happens, I just get looped back to the login.aspx 
page :(

The following I created accordingly (also the database table that is on 
the site there.

If someone can help me out here, that would be greatly appreciated.

Ewout!

================
DEFAULT.ASPX
================
Nothing in here, just a page to test if I get redirect, which I 
get...when loading this page I go to the Login.aspx.


================
LOGIN.ASPX
================
<%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-
1" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Web.Security" %>
<script language="C#" runat="server">
private bool ValidateUser(string uid, string passwd)
{
   SqlConnection cnn;
   SqlCommand cmd;
   SqlDataReader dr;
   cnn = new SqlConnection
("server=myserver;uid=myuid;pwd=mypassword;database=mydatabase");
   cmd = new SqlCommand("Select *from users where uname='" + uid 
+ "'",cnn);
   cnn.Open();
   dr = cmd.ExecuteReader();
   while (dr.Read())
   {
		
      if (string.Compare(dr["Pwd"].ToString(),passwd,false)==0)
      {
         cnn.Close();
         return true;
      }
   }
   cnn.Close();
   return false;
}

private void cmdLogin_ServerClick(object sender, System.EventArgs e)
{
if (ValidateUser(txtUserName.Value,txtUserPass.Value) )
	FormsAuthentication.RedirectFromLoginPage(txtUserName.Value,
		chkPersistCookie.Checked);
	else
		Response.Redirect("login.aspx", true);
}
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server">
<h3>
   <font face="Verdana">Logon Page</font>
</h3>
<table>
   <tr>
      <td>Email:</td>
      <td><input id="txtUserName" type="text" runat="server"></td>
      <td><ASP:RequiredFieldValidator ControlToValidate="txtUserName"
           Display="Static" ErrorMessage="*" runat="server" 
           ID="vUserName" /></td>
   </tr>
   <tr>
      <td>Password:</td>
      <td><input id="txtUserPass" type="password" runat="server"></td>
      <td><ASP:RequiredFieldValidator ControlToValidate="txtUserPass"
          Display="Static" ErrorMessage="*" runat="server" 
          ID="vUserPass" />
      </td>
   </tr>
   <tr>
      <td>Persistent Cookie:</td>
      <td><ASP:CheckBox id="chkPersistCookie" runat="server" 
autopostback="false" /></td>
      <td></td>
   </tr>
</table>
<input type="submit" Value="Logon" runat="server" ID="cmdLogin"><p></p>
<asp:Label id="lblMsg" ForeColor="red" Font-Name="Verdana" Font-Size="10" 
runat="server" />
</form>
</body>
</html>

  Return to Index