locked
Login RRS feed

  • Question

  • User-1499457942 posted

    Hi

      I have below Web.Config .  I am not using default Login Form. How to check on Pages.

    <authentication mode="Forms">
    		<forms defaultUrl="~/Default.aspx" loginUrl="~/Login.aspx" slidingExpiration="true" timeout="2880"></forms>
        </authentication>

    In Login I have below code
    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Status.ToString(), DateTime.Now, DateTime.Now.AddMinutes(100), false, UserName.ToString(), FormsAuthentication.FormsCookiePath);
    string st = FormsAuthentication.Encrypt(ticket);
    HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, st);
    Response.Cookies.Add(cookie);
    Response.Redirect("Default.aspx");

    Thanks

    Friday, August 3, 2018 9:28 AM

All replies

  • User475983607 posted

    JagjitSingh

    I am not using default Login Form. How to check on Pages.

    You question is unclear.  My best guess is you want to check if the user is authenticated using ASP Forms Authentication.

    User.Identity.IsAuthenticated

    To get the username.

    User.Identity.Name

    The code is using status string as the username which is odd at best...  You'll need to explain why,,,

    FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, Status.ToString(), DateTime.Now, DateTime.Now.AddMinutes(100), false, UserName.ToString(), FormsAuthentication.FormsCookiePath);

    The Forms Authentication documentation, which is openly published, has everything you need.  I recommend reading the docs and using the API as suggested.

    https://msdn.microsoft.com/en-us/library/system.web.security.formsauthenticationticket(v=vs.110).aspx

    For some reason unknown reason you placed the username in the data data section. To get the user data property, see the following code.

    FormsIdentity identity = (FormsIdentity)Context.User.Identity;
    userData = identity.Ticket.UserData;

    I recommend following the standards.  Place the username in the username field of the Forms Authentication Ticket.  

    Friday, August 3, 2018 10:39 AM
  • User-1171043462 posted

    Refer

    Simple User Login Form example in ASP.Net

    Friday, August 3, 2018 12:24 PM
  • User-1499457942 posted

    Hi

      I don't want to use asp.net Login control

    Thanks

    Friday, August 3, 2018 12:38 PM
  • User475983607 posted

    JagjitSingh

    I don't want to use asp.net Login control

    Completely Irrelevant.  The login control is just a control.  Simply replace the login control with a button click handler as clearly and openly shown in the linked documentation.  This is very basic stuff...

    Please read the reference documentation and follow standard practices. 

    Friday, August 3, 2018 1:03 PM
  • User-1171043462 posted

    Still you can use my article: Simple User Login Form example in ASP.Net

    protected void ValidateUser(object sender, EventArgs e)
    {
        int userId = 0;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand("Validate_User"))
            {
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.Parameters.AddWithValue("@Username", txtUserName.Text);
                cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
                cmd.Connection = con;
                con.Open();
                userId = Convert.ToInt32(cmd.ExecuteScalar());
                con.Close();
            }
            switch (userId)
            {
                case -1:
                    lblError.Text = "Username and/or password is incorrect.";
                    break;
                case -2:
                    lblError.Text = "Account has not been activated.";
                    break;
                default:
                    FormsAuthentication.RedirectFromLoginPage(txtUserName.Text, true);
                    break;
            }
        }
    }
    

    HTML

    <asp:TextBox runat="server" ID="txtUserName"></asp:TextBox>
    <asp:TextBox runat="server" ID="txtPassword" TextMode="Password"></asp:TextBox>
    <asp:Button runat="server" ID="btnLogin" Text = "Login" OnClick="ValidateUser"/>
    <asp:Label ID = "lblError" runat = "server" />

    Code

    Friday, August 3, 2018 1:41 PM
  • User-1499457942 posted

    Hi

      I have written below code on Login Button Click . Is it ok . Secondly i want to know what the below 2 lines do

    FormsIdentity identity = (FormsIdentity)Context.User.Identity;
    string userData = identity.Ticket.UserData;

    FormsIdentity identity = (FormsIdentity)Context.User.Identity;
                        string userData = identity.Ticket.UserData;
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,UserName,DateTime.Now,DateTime.Now.AddMinutes(30),isPersistent,userData,FormsAuthentication.FormsCookiePath);
                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                        Response.Redirect("Default.aspx");
    
    On Default.aspx i have written below code
    if (!this.Page.User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.RedirectToLoginPage();
                }

    Thanks

    Friday, August 3, 2018 5:09 PM
  • User475983607 posted

    Hi

      I have written below code on Login Button Click . Is it ok . Secondly i want to know what the below 2 lines do

    FormsIdentity identity = (FormsIdentity)Context.User.Identity;
    string userData = identity.Ticket.UserData;

    FormsIdentity identity = (FormsIdentity)Context.User.Identity;
                        string userData = identity.Ticket.UserData;
                        FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,UserName,DateTime.Now,DateTime.Now.AddMinutes(30),isPersistent,userData,FormsAuthentication.FormsCookiePath);
                        string encTicket = FormsAuthentication.Encrypt(ticket);
                        Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket));
                        Response.Redirect("Default.aspx");
    
    On Default.aspx i have written below code
    if (!this.Page.User.Identity.IsAuthenticated)
                {
                    FormsAuthentication.RedirectToLoginPage();
                }

    Thanks

    The two lines cause a null exception error since the forms ticket has not been created. 

    If the two lines are implemented after the ticket is created, the code fetches the userdata from the forms ticket as explained in my first thread.  You can find this same information in the posted link, but it does require that you click the link and actually read the document.

    Friday, August 3, 2018 5:32 PM