How to Create cookie in Visual C# 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ssmeshack
    New Member
    • Jul 2008
    • 38

    How to Create cookie in Visual C# 2008

    Hai All,

    Im doing a login page.... And that was without cookie....
    I realy dono how to create cookie...
    pls anyone can help...

    Here is code from Default.aspx for login control:

    [HTML]<asp:Login ID="Login1" runat="server" BackColor="#F7F 6F3" BorderColor="#E 6E2D8"
    BorderPadding=" 4" BorderStyle="So lid" BorderWidth="1p x" Font-Names="Verdana"
    Font-Size="0.8em" ForeColor="#333 333" onauthenticate= "Login1_Authent icate"
    CreateUserText= "Create User" CreateUserUrl=" ~/UserControl/AddStaff.ascx"
    Height="162px" Width="292px">
    <TextBoxStyle Font-Size="0.8em" />
    <LoginButtonSty le BackColor="#FFF BFF" BorderColor="#C CCCCC" BorderStyle="So lid"
    BorderWidth="1p x" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284 775" />
    <InstructionTex tStyle Font-Italic="True" ForeColor="Blac k" />
    <TitleTextSty le BackColor="#5D7 B9D" Font-Bold="True" Font-Size="0.9em"
    ForeColor="Whit e" />
    </asp:Login>[/HTML]


    And here is the code behind.... Default.aspx.cs


    [HTML]using System;
    using System.Collecti ons;
    using System.Configur ation;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.H tmlControls;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Xml.Linq ;
    using System.Data.Sql Client;

    public partial class test : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {

    }
    protected void Login1_Authenti cate(object sender, AuthenticateEve ntArgs e)
    {
    bool Authenticated = false;
    Authenticated = SiteLevelCustom AuthenticationM ethod(Login1.Us erName, Login1.Password );
    e.Authenticated = Authenticated;
    if (Authenticated == true)
    {

    Response.Redire ct("Dashboard2. aspx");
    }
    }

    private bool SiteLevelCustom AuthenticationM ethod(string UserName, string Password)
    {
    bool boolReturnValue = false;
    // Insert code that implements a site-specific custom
    // authentication method here.
    // This example implementation always returns false.
    System.Data.Sql Client.SqlConne ction conn = new SqlConnection(" Data Source=(local); Initial Catalog=DBhelpd esk; Integrated Security=SSPI;" );
    conn.Open();
    System.Data.Sql Client.SqlComma nd comm = new SqlCommand("sp_ ValidateUser", conn);

    comm.Parameters .AddWithValue(" @UserName", UserName);
    comm.Parameters .AddWithValue(" @Password", Password);

    comm.CommandTyp e = CommandType.Sto redProcedure;
    SqlDataReader Dr;


    Dr = comm.ExecuteRea der();

    while (Dr.Read())
    {
    boolReturnValue = true;
    }

    Dr.Close();
    return boolReturnValue ;

    }
    }[/HTML]
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    What kind of cookie do you want?
    The Request and Response objects have a CookieCollectio n for you to set and view cookies.

    Comment

    • ssmeshack
      New Member
      • Jul 2008
      • 38

      #3
      Hi,

      The cookie I want is for login.
      Save the user info to cookie - StaffID or UserName
      Can you help me with this?
      tq.

      From,
      Meshack

      Originally posted by Plater
      What kind of cookie do you want?
      The Request and Response objects have a CookieCollectio n for you to set and view cookies.

      Comment

      • ssmeshack
        New Member
        • Jul 2008
        • 38

        #4
        Hi All,

        I have done a bit changes and add cookie in login page and read cookie "UserName" in Dashboard2.aspx .
        But there is a problem logout... because the cookie still exist and read even i did't login. Is there any code to kill or erase the cookie while logout?

        [HTML]public partial class Dashboard2 : System.Web.UI.P age
        {
        protected void Page_Load(objec t sender, EventArgs e)
        {

        if (Request.Cookie s["UserName"] == null)
        {
        //The cookie is not available go on without it.
        AuthenticatedMe ssagePanel.Visi ble = false;

        AnonymousMessag ePanel.Visible = true;
        }
        else
        {
        //Cookie is still there let's read it.
        string sUserName = Request.Cookies["UserName"].Value;
        Label1.Text = "Welcome back, " + sUserName + " !";

        AuthenticatedMe ssagePanel.Visi ble = true;

        AnonymousMessag ePanel.Visible = false;
        }

        }[/HTML]

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Why not just use the Session object, that's what its there for

          Comment

          • ssmeshack
            New Member
            • Jul 2008
            • 38

            #6
            Hi,

            Im already done with the cookie. I just wana use the to remember the user that login. Delete the user's cookie when logout. Thats all. Thank you anyway.

            Thanks.

            Originally posted by Plater
            Why not just use the Session object, that's what its there for

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Originally posted by ssmeshack
              Hi,

              Im already done with the cookie. I just wana use the to remember the user that login. Delete the user's cookie when logout. Thats all. Thank you anyway.

              Thanks.
              That is EXACTLY what the session is used for....

              Comment

              • Curtis Rutland
                Recognized Expert Specialist
                • Apr 2008
                • 3264

                #8
                Originally posted by Plater
                That is EXACTLY what the session is used for....
                Listen to him. Session is made to store objects for the duration of a user's session. Why try to shoehorn cookies into something they're not, when you have an object tailor made to your needs?

                Comment

                • ssmeshack
                  New Member
                  • Jul 2008
                  • 38

                  #9
                  Hi All,

                  Thank you for your advises...
                  I will try to do using session...
                  Thank you.

                  Comment

                  Working...