DEV Community

harsh123
harsh123

Posted on

How to make login in .NET

Login.cs

    using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace login
{
    public partial class login : System.Web.UI.Page
    {
        public object LoginStatus1 { get; private set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) 
            {
                int ViewstateVal = 1;


                if (ViewState["count"] != null)
                {
                    ViewstateVal = Convert.ToInt32(ViewState["count"]) + 1;

                    ViewState["count"] = ViewstateVal.ToString();
                }
                else { ViewState["count"] = "1"; }

                TextBox1.Text = ViewstateVal.ToString();
                ViewState["count"] = ViewstateVal.ToString();
            }

        }

        protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            bool verify = false;
            verify = MyAuthenticationLogic(Login1.UserName, Login1.Password);
            e.Authenticated = verify;
            if (e.Authenticated == true)
            {

                HttpCookie c1 = new HttpCookie("loginInfo");
                c1["Name"] = Login1.UserName;
                c1["Password"] = Login1.Password;
                Response.Cookies.Add(c1);

                Server.Transfer("redirect.aspx?username=" + Login1.UserName + "&password=" + Login1.Password, true);
            }
            else{ 
                errMsg.LoginText = "Enter the correct Credentials"; 
            }
        }
        protected bool MyAuthenticationLogic(string username, string password)
        {
            if (username == "Hrushikesh" && password == "CodeMaster")
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        protected void btn1_Click(object sender, EventArgs e)
        {
            Server.Transfer("redirect.aspx?username=" + Login1.UserName + "&password=" + Login1.Password, false);
        }
    }

}
Enter fullscreen mode Exit fullscreen mode

Register.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace login
{
    public partial class redirect : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = Request.QueryString["Username"];
            string password = Request.QueryString["password"];

            msgTxt.Text = "Welcome " + username + " Your Password is : " + password;

            HttpCookie c1 = Request.Cookies["loginInfo"];
            lbl1.Text ="Passord Is : "+ c1["Password"];
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

ScreenShots

Image description

Image description

Read More

Top comments (0)