如果读者在Sitemaster上阅读

时间:2014-12-28 14:32:55

标签: c# sql asp.net cookies login

好吧,我试图在网站管理员上登录,它工作正常..但在登录时我想要一个标签存储的东西..并且有些东西消失了

这是我的网站站长代码..

<form id="Form1" runat="server">
    <div class="wrap">
        <div class="headerwrap">
            <div class="logo"><a href="<%= Page.ResolveUrl("~/Default.aspx") %>"><img width="70%" height="85%" src="<%= Page.ResolveUrl("~/images/hiremelogo.png") %>" /></a></div>
            <div id="container">
            <a href="<%= Page.ResolveUrl("~/Default.aspx?logout=1") %>"><div class="logout" id="logout" runat="server"><div class ="text">Logout</div></div></a>

            <!-- Login Starts Here -->
            <div runat="server" id="loginContainer">
                <a href="#" id="loginButton"><span>Login</span><em></em></a>
                <div style="clear:both"></div>
                <div id="loginBox">        
                <div class="loginForm">        
                    <form id="loginForm">
                        <fieldset id="body" class="bodyy">
                            <fieldset>
                                <label for="email">Username</label>
                                <asp:TextBox name="email" id="email" type="text" runat="server"></asp:TextBox>
                            </fieldset>
                            <fieldset>
                                <label for="password">Password</label>
                                <asp:TextBox type="password" name="password" id="password" runat="server"></asp:TextBox>
                            </fieldset>
                            <asp:Button id="login" class="loginbutton" runat="server" Text="Sign in" onclick="login_Click" />
                        </fieldset>
                        <span><a href="#">Forgot your password?</a></span>
                    </form>
                    </div>
                </div>
            </div>
            <!-- Login Ends Here -->
        </div>
        </div>

        <div id="div1" runat="server"></div>
        <div id="div2" runat="server"></div>
        <div id="div3" runat="server"></div>

        <div class="menuwrap">
        <div class="menu_simple">
        <ul>
            <li><a href="#">Home</a></li>
            <li><a href="#">Jobs</a></li>
            <li><a href="#">Seekers</a></li>
            <li><a href="#">Employers</a></li>
            <li><a href="#">News</a></li>
            <li><a href="#">About Us</a></li>
            <li><a href="#">Contact Us</a></li>
        </ul>
        </div>
        </div>



            <asp:ContentPlaceHolder ID="MainContent" runat="server"/>


        <div class="breakline"></div>

        <div class="footerwrap">
        <div id="asdasd" runat="server"></div>
            <asp:Label ID="Label1" runat="server"></asp:Label>
        </div>

    </div>
    </form>

我的C#代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class hireme : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request.QueryString["logout"] != null)
        {
            Response.Cookies["UserSettings"]["admin1"] = "";
            Response.Cookies["UserSettings"]["admin2"] = "";
            Response.Cookies["UserSettings"]["username"] = "";
            Response.Cookies["UserSettings"]["username1"] = "";
            Response.Cookies["UserSettings"]["sid"] = "";
            Response.Cookies["UserSettings"]["eid"] = "";
            Response.Redirect(Request.ServerVariables["SCRIPT_NAME"].ToString());
        }

    }
    protected void login_Click(object sender, EventArgs e)
    {


        if (USER(email.Text).Equals(true))
        {
            SqlConnection myConn, myConn2;
            SqlCommand myCommand, myCommand2;
            SqlDataReader myReader, myReader2;
            String SQL, SQL2;
            myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());
            myConn2 = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());

            myConn.Open();
            myConn2.Open();
            SQL = "SELECT * FROM seekers WHERE s_username = @user AND s_password = @pass";
            SQL2 = "SELECT * FROM employers WHERE e_username = @user AND e_password = @pass";
            myCommand = new SqlCommand(SQL, myConn);
            myCommand2 = new SqlCommand(SQL2, myConn2);
            myCommand.Parameters.AddWithValue("@user", email.Text);
            myCommand.Parameters.AddWithValue("@pass", password.Text);
            myCommand2.Parameters.AddWithValue("@user", email.Text);
            myCommand2.Parameters.AddWithValue("@pass", password.Text);
            myReader = myCommand.ExecuteReader();
            myReader2 = myCommand2.ExecuteReader();


            if (myReader.Read())
            {

                div1.InnerText = "Login Success!";
                Response.Cookies["UserSettings"]["username"] = email.Text;
                Response.Cookies["UserSettings"]["sid"] = myReader["s_id"].ToString();
                Response.Cookies["UserSettings"]["eid"] = "0";
                logout.Visible = false;
                Label1.Text = "TEST";
                Response.Redirect("~/Default.aspx");


            }
            else
            {
                asdasd.InnerText = "S Fail";
                div1.Visible = true;
                div2.Visible = false;
                div3.Visible = false;
                div1.InnerHtml = "<br><div class='alert alert-error'>Invalid Username/Password!</div>";
            }



            myConn.Close();


        }
        else
        {
            asdasd.InnerText = "Full Fail";
            div1.Visible = true;
            div2.Visible = false;
            div3.Visible = false;
            div1.InnerHtml = "<br><div class='alert alert-error'>You are not Registered! Please Register first to use the Service</div>";
        }

    }

    public Boolean USER(String username)
    {
        SqlConnection myConn;
        SqlCommand myCommand;
        SqlDataReader myReader;
        String SQL;
        myConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ApplicationServices"].ToString());

        myConn.Open();
        SQL = "SELECT * FROM seekers WHERE s_username = @user";
        myCommand = new SqlCommand(SQL, myConn);
        myCommand.Parameters.AddWithValue("@user", email.Text);
        myReader = myCommand.ExecuteReader();

        if (myReader.Read())
        {
            myConn.Close();
            return true;
        }
        else
        {
            myConn.Close();
            return false;
        }
    }


}

嗯,登录工作正常,我可以在我的默认页面上检索cookie值,问题是Label1没有更新,注销仍然可见..

我不确定我在这里做错了什么..

任何帮助都是可能的......!

1 个答案:

答案 0 :(得分:1)

您的登录程序以重定向到另一页结束:

Response.Redirect("~/Default.aspx");

在当前页面上设置值,不会执行任何操作。浏览器只会重定向到默认页面。

解决方案是检查默认页面的负载是否用户已登录并通过显示正确的面板做出反应。