locked
Login Authentication RRS feed

  • Question

  • User-797751191 posted

    Hi

      I have 2 Web Forms . In one Form i want to ensure that user must Login . On 1 page the user can access directly.

    Thanks

    Friday, June 21, 2019 7:46 AM

All replies

  • User-1038772411 posted

    Hi, jsShivalik

    You can use masterpage and than create two child page in it. (webform1 and webform2).

    in masterpage on load you can check userid==null or not if userid returns null than redirect to login.

    after login success you will get loginid means userid or as you want to add in your table. just keep and "Set as Hidden Field" for other pages. so after than we you call your secound webform call masterpage event and you will get userid so thats sinerio you can not direct to login. thats way you can access directly webforms.

    Thanks

    Friday, June 21, 2019 8:00 AM
  • User288213138 posted

    Hi jsshivalik,

    You can use the Login control directly.

    The login control provides user interface  elements for logging in to a Web site.

    More information about login control you can reference this link:

    https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.login?view=netframework-4.7.2

    I wrote a demo that you can refer to.

    The code:

    <div>
                <asp:Login ID="Login1" runat="server" OnAuthenticate="Login1_Authenticate"></asp:Login>
            </div>
    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
            {
                if (e.Authenticated)
                {
                    Response.Redirect("https://forums.asp.net/t/2156936.aspx");
                }
                if (ValidateUser(Login1.UserName, Login1.Password))
                {
                    Response.Redirect("https://forums.asp.net/t/2156936.aspx");
                }
                else
                {
                   e.Authenticated = false;
                }         
            }
            private bool ValidateUser(string username,string password)
            {
                bool status;
                string constr = ConfigurationManager.ConnectionStrings["constr527"].ConnectionString;
                SqlConnection con = new SqlConnection(constr);
                string query = "Select * from Students";
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = query;
                cmd.Connection = con;
                SqlDataAdapter adapter = new SqlDataAdapter();
                adapter.SelectCommand = cmd;
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                string uname;
                string pass;
                uname = ds.Tables[0].Rows[0]["Name"].ToString();
                pass= ds.Tables[0].Rows[0]["Password"].ToString();
                Response.Write(uname);
                Response.Write(pass);
                con.Close();
                if(uname== username && pass== password)
                {
                    Session[""]= uname;
                    status = true;
                }
                else
                {
                    status = false;
                }
                return status;
            }
    

    The result:

    Best regards,

    Sam

    Monday, June 24, 2019 12:30 PM