<PRE lang=html><%@ Page Language="C#" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<html>
<head>
<title>Login</title>
</head>
<script runat="server">
// If you're using code-behind, make sure you change "private" to
// "protected" since the .aspx page inherits from the .aspx.cs
// file's class
private void btnLogin_Click(Object sender, EventArgs e)
{
// Initialize FormsAuthentication, for what it's worth
FormsAuthentication.Initialize(); // Create our connection and command objects
SqlConnection conn =
 new SqlConnection("Data Source=localhost;Initial Catalog=web;");
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT roles FROM web WHERE username=@username " +
 "AND password=@password"; // Fill our parameters
cmd.Parameters.Add("@username", SqlDbType.NVarChar, 64).Value =
Username.Value;
cmd.Parameters.Add("@password", SqlDbType.NVarChar, 128).Value =
 FormsAuthentication.HashPasswordForStoringInConfigFile(
Password.Value, "md5"); // Or "sha1" // Execute the command
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
if (reader.Read())
{
 // Create a new ticket used for authentication
 FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1, // Ticket version
Username.Value, // Username associated with ticket
DateTime.Now, // Date/time issued
DateTime.Now.AddMinutes(30), // Date/time to expire
true, // "true" for a persistent user cookie
reader.GetString(0), // User-data, in this case the roles
FormsAuthentication.FormsCookiePath);// Path cookie valid for  // Encrypt the cookie using the machine key for secure transport
 string hash = FormsAuthentication.Encrypt(ticket);
 HttpCookie cookie = new HttpCookie(
FormsAuthentication.FormsCookieName, // Name of auth cookie
hash); // Hashed ticket  // Set the cookie's expiration time to the tickets expiration time
 if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;  // Add the cookie to the list for outgoing response
 Response.Cookies.Add(cookie);  // Redirect to requested URL, or homepage if no previous page
 // requested
 string returnUrl = Request.QueryString["ReturnUrl"];
 if (returnUrl == null) returnUrl = "/";这里是什么意思了,我只能懂的字面意思。定向到被请求得Url,这里指得是什么牙,谢谢  // Don't call FormsAuthentication.RedirectFromLoginPage since it
 // could
 // replace the authentication ticket (cookie) we just added
 Response.Redirect(returnUrl);
}
else
{
 // Never tell the user if just the username is password is incorrect.
 // That just gives them a place to start, once they've found one or
 // the other is correct!
 ErrorLabel = "Username / password incorrect. Please try again.";
 ErrorLabel.Visible = true;
} reader.Close();
conn.Close();
}
</script>
<body>
<p>Username: <input id="Username" runat="server"
type="text"/><br />
Password: <input id="Password" runat="server" type="password"/><br
/>
<asp:Button id="btnLogin" runat="server" OnClick="btnLogin_Click"
 Text="Login"/>
<asp:Label id="ErrorLabel" runat="Server" ForeColor="Red"
 Visible="false"/></p>
</body>
</html></PRE>

解决方案 »

  1.   

     // Redirect to requested URL, or homepage if no previous page
     // requested
     string returnUrl = Request.QueryString["ReturnUrl"];
     if (returnUrl == null) returnUrl = "/";这里是什么意思了,我只能懂的字面意思。定向到被请求得Url,这里指得是什么牙,谢谢
      

  2.   

    反斜杠 / 是站点跟目录
    如果接收的Request.QueryString["ReturnUrl"]没有任何东西就把returnUrl 设置为跟目录
      

  3.   

    /  是站点根目录(域名后面不带文件夹)
    ~/  是应用程序根目录(就是bin的父文件夹)
    如果bin就在站点根下,那么这两个就一样了.