2

我的 site.master 设计中有以下 loginview 代码

<asp:LoginView ID="HeadLoginView" runat="server" EnableViewState="false">
                <AnonymousTemplate>
                    [ <a href="Log.aspx" ID="HeadLoginStatus" runat="server">Log In</a> ]
                </AnonymousTemplate>
                <LoggedInTemplate>
                    Welcome <span class="bold"><asp:LoginName ID="HeadLoginName" runat="server" /></span>!
                    [ <asp:LoginStatus ID="HeadLoginStatus" runat="server" LogoutAction="Redirect" LogoutText="Log Out" LogoutPageUrl="~/"/> ]
                </LoggedInTemplate>
            </asp:LoginView>

log.aspx.cs 文件具有以下代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        Master.FindControl("CAMenu").Visible = false;
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        if (txtUsername.Text == "admin" && txtPassword.Text == "1234")
        {
            Response.Redirect("NewEvent.aspx");
        }
    }

但是登录视图没有更新为欢迎“用户名”..这里有什么错误。我应该改变什么吗???请帮忙

4

3 回答 3

1

In Login.Aspx i made the following changes..

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Master.FindControl("CAMenu").Visible = false;
            Master.FindControl("loginStatus").Visible = false;
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }
    protected void LoadMessageBox(string MessageID)
    {
        try
        {
            messages = new GUIMessages();
            popupExtend = new ModalPopupExtender();
            lbMessage = (Label)Master.FindControl("label5");
            lbMessage.Text = messages.GetGUIMessage(GUIModule.Login, MessageID);
            popupExtend = (ModalPopupExtender)Master.FindControl("popupExtender");
            popupExtend.Show();
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.LoadMessageBox(string MessageID)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }

    protected void btnLogin_Click(object sender, EventArgs e)
    {
        try
        {
            string userName = txtUsername.Text;
            string password = txtPassword.Text;

            if (userName == string.Empty && password == string.Empty)
            {
                LoadMessageBox("5");
                txtUsername.Focus();


                return;
            }
            if (userName == string.Empty)
            {
                LoadMessageBox("1");
                txtUsername.Focus();
                return;
            }
            else if (password == string.Empty)
            {
                LoadMessageBox("3");

                txtPassword.Focus();
                return;
            }

            User user = new User(userName);
            DataTable tab = new DataTable();
            tab = user.GetUserDetails(userName);
            string firstName = string.Empty;
            string userPassword = string.Empty;
            string RoleID = string.Empty;
            string userID = string.Empty;
            Session["UserName"] = userName;
            if (tab.Rows.Count == 0)
            {
                LoadMessageBox("6");
                txtPassword.Text = string.Empty;
                txtUsername.Text = string.Empty;
                txtUsername.Focus();
                return;
            }

            if (tab.Rows.Count == 1)
            {
                userID = tab.Rows[0][0].ToString();
                firstName = tab.Rows[0][1].ToString();
                userPassword = tab.Rows[0][2].ToString();
                RoleID = tab.Rows[0][3].ToString();
                Session["UserID"] = userID;
            }

                if (userPassword == password)
                {
                    Response.Redirect("~/Default.aspx");
                }
                else
                {
                    LoadMessageBox("4");
                    txtPassword.Focus();
                    return;
                }

        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.Login.btnLogin_Click(object sender, EventArgs e)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }

And in Site.Master:-

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Page.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            if (Page.Title != "Login")
            {
                if (Session.Count == 0 || Session["Username"] == null)
                    Response.Redirect("~/Login.aspx", true);
                CheckRole();

            }
            lblDateTime.Text = "";
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.SiteMaster.Page_Load()(object sender, EventArgs e)", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
      }

    public void CheckRole()
    {
        try
        {
            if (System.Web.HttpContext.Current.Session.Count > 0)
            {
                string firstName = string.Empty;
                // string userPassword = string.Empty;
                string RoleID = string.Empty;
                Common common = new Common();
                DataTable tab = new DataTable();
                string userName = (string)Session["UserName"];
                User user = new User(userName);
                tab = user.GetUserDetails(userName);

                if (tab.Rows.Count == 1)
                {
                    firstName = tab.Rows[0][1].ToString();
                    RoleID = tab.Rows[0][3].ToString();
                }

                if (RoleID != "1")
                {
                    int count = CAMenu.Items.Count;
                    if (count == 5)
                    {
                        for (int menuCount = 3; menuCount > 0; menuCount--)
                        {
                            string text = CAMenu.Items[menuCount - 1].Text;
                            CAMenu.Items.RemoveAt(menuCount - 1);
                        }
                    }
                    lbLoginMessage.Text = "Welcome," + " " + firstName;
                    loginStatus.Visible = true;
                }
                else
                {
                    lbLoginMessage.Text = "Welcome," + " " + firstName;
                    loginStatus.Visible = true;
                }

            }
            else
            {
                Session.Abandon();
                Response.Redirect("~/Login.aspx", true);
            }
        }
        catch (Exception ex)
        {
            new Logger().Log("ShortCom.SiteMaster.CheckRole()", ex.Message);
            Response.Redirect("~/Error.aspx");
        }
    }
于 2012-05-24T09:19:23.207 回答
1

使用登录控件 onauthenticate 事件:

protected void OnAuthenticate(object sender, AuthenticateEventArgs e)
    {
 if (txtUsername.Text == "admin" && txtPassword.Text == "1234")
        {
            e.Authenticated = true;
        }
        else {
            e.Authenticated = false;
        }    

}
于 2012-04-19T10:52:23.827 回答
0

尝试这个...

将此添加到您的登录控件验证事件处理程序...(请在 VB中)

If Membership.ValidateUser(login2.UserName, login2.Password) Then

            If Not Request.QueryString("ReturnUrl") Then

                FormsAuthentication.RedirectFromLoginPage(login2.UserName, False)

            Else

                FormsAuthentication.SetAuthCookie(login2.UserName, False)
            End If

        Else
            Response.Write("Invalid UserID and Password")
        End If

用于发布事件处理的新控件必须覆盖登录控件身份验证事件的默认事件处理。希望它有效..谢谢。

于 2016-01-30T13:55:46.987 回答