如题

解决方案 »

  1.   

    我有:
    log.ascx:
    <%@ Control Language="c#" AutoEventWireup="false" Codebehind="log.ascx.cs" Inherits="myEdu.log" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
    <table cellSpacing="1" cellPadding="1" width="100%" border="0">
    <% 
                   if(Request.Cookies["UserInfo"] == null)
                          {
            %>
    <tr>
    <td align="right" width="40%">用户名:</td>
    <td><asp:textbox id="txtName" Runat="server" Height="20px" Width="80px" CssClass="inputSel"></asp:textbox></td>
    </tr>
    <tr>
    <td align="right" width="40%">密码:</td>
    <td>
    <asp:textbox id="txtPass" runat="server" TextMode="Password" Height="20px" Width="80px" CssClass="inputSel"></asp:textbox>
    </td>
    </tr>
    <tr>
    <td align="right" width="40%">登陆信息:</td>
    <td style="FONT-SIZE: 8pt; FONT-FAMILY: 宋体, Arial">
    <asp:dropdownlist id="dListCookie" runat="server" Height="20px" Width="81px" Font-Size="9pt" Font-Names="宋体,Arial">
    <asp:ListItem Value="0" Selected="True">不保存</asp:ListItem>
    <asp:ListItem Value="1">保存一周</asp:ListItem>
    </asp:dropdownlist></td>
    </tr>
    <tr>
    <td width="100%" height="5" colspan="2"></td>
    </tr>
    <tr>
    <td align="right" width="50%">
    <asp:imagebutton id="btnLogin" runat="server" ImageUrl="image/login.gif"></asp:imagebutton>&nbsp;</td>
    <td align="left">&nbsp;
    <asp:imagebutton id="btnReg" runat="server" ImageUrl="image/reg.gif"></asp:imagebutton></td>
    </tr>
    <%            }
                   else
                   {
            %>
    <tr>
    <td colspan="2">
    &nbsp;&nbsp;<b><font color="#ff0000">
    <%=Request.Cookies["UserInfo"]["UserName"]%>
    : </font></b></FONT></B> 您好!欢迎登陆</td>
    </tr>
    <tr>
    <td align="center" colspan="2"><b>祝您学习愉快!</b></td>
    </tr>
    <tr>
    <td width="50%" align="center">帐户到期:</td>
    <td><font color=#ff0000><% if(System.DateTime.Parse(Request.Cookies["UserInfo"]["OverDate"])<System.DateTime.Now){%>
    已到期<%}else{%><%=System.DateTime.Parse(Request.Cookies["UserInfo"]["OverDate"]).ToShortDateString()%><%}%></font></td>
    </tr>
    <tr>
    <td width="50%" align="center"><a href="logout.aspx">安全退出</a></td>
    <td align="center"><a href="userinfo.aspx" target="_blank">个人信息</a></td>
    </tr>
    <tr>
    <td width="50%" align="center"><a href="full.aspx" target="_blank" style="COLOR: #ff0000">帐户充值</a></td>
    <td align="center">短消息</td>
    <%             }
        %>
    </tr>
    </table>//log.ascx.cs:
    namespace myEdu
    {
    using System;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Data.OleDb;
    using System.Configuration;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using Pwqzc; /// <summary>
    /// log 的摘要说明。
    /// </summary>
    public class log : System.Web.UI.UserControl
    {
    protected System.Web.UI.WebControls.TextBox txtName;
    protected System.Web.UI.WebControls.TextBox txtPass;
    protected System.Web.UI.WebControls.DropDownList dListCookie;
    protected System.Web.UI.WebControls.ImageButton btnLogin;
    protected System.Web.UI.WebControls.ImageButton btnReg; private void Page_Load(object sender, System.EventArgs e)
    {
    // 在此处放置用户代码以初始化页面
    if(!IsPostBack)
    {
    //把用户最后的一个请求页面放到cookies里面保存起来
    Response.Cookies["LastUrl"].Value = Request.Url.ToString();
    if(Request.Cookies["UserInfo"]==null)
    {
         SetFocus(txtName);//如果用户没有登陆就把名字栏获得焦点以便输入
    }
    }
    } #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器
    /// 修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.btnLogin.Click += new System.Web.UI.ImageClickEventHandler(this.btnLogin_Click);
    this.btnReg.Click += new System.Web.UI.ImageClickEventHandler(this.btnReg_Click);
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    public void SetFocus(System.Web.UI.Control control)
    {
    /* 1、编写: 
    * 2、功能:让控件得到焦点
    * 3、参数:System.Web.UI.Control control 任意已实例化的控件
    * 4、返回值: 无
    * 5、用途:使传入该方法的控件得到焦点
    */
    if (HttpContext.Current.Request.Browser.JavaScript)
    {
    control.Page.RegisterStartupScript("sf","<script language='javascript'>document.forms[0]." +
    control.ClientID +".focus();</script>");
    }
    }
    private void btnReg_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    Response.Redirect("register.aspx");
    } private void btnLogin_Click(object sender, System.Web.UI.ImageClickEventArgs e)
    {
    if(txtName.Text.Trim()=="" || txtPass.Text.Trim()=="")
    {
    this.Page.rRegisterStartupScript("js","<script language='javascript'>alert('用户名或密码不能为空!');</script>");
    }
    else
    {
    MyDataBase db = new MyDataBase();
    if(db.Login(txtName.Text,txtPass.Text))
    {
    Response.Cookies["UserInfo"]["UserName"] = txtName.Text;
    Response.Cookies["UserInfo"]["OverDate"] = db.GetOverDate(txtName.Text,txtPass.Text).ToString();;
    if(dListCookie.SelectedItem.Value=="1")
    {
    //Response.Cookies["UserInfo"]["UserName"]=txtName.Text;
    Response.Cookies["UserInfo"].Expires = DateTime.Now.AddDays(7);
    }
    Response.Redirect(Request.Url.ToString());//重新导向到原请求页面
    }
    else
    {
    Response.Write("<script>alert('登陆失败!用户名或者密码错误!');</script>");
    }
    }
    }
    }
    }
    给份吧
      

  2.   

    配置文件设为:
    <authentication mode="Forms">
      <forms name="XsxxglAuth" loginUrl="login.aspx" timeout="30"/>
    </authentication>
    <authorization>
      <deny users="?" /> 
    </authorization>
    密码验证后写入:
    FormsAuthentication.SetAuthCookie(userCode,false);