Mohd Aamir

Mohd Aamir

  • NA
  • 68
  • 4k

without login another pages should not be opened on browser

Jan 12 2019 12:49 AM
my problem is that my all the pages are being accessed without login but i want that it should be accessed untill you don't login and it should be first login then access any pages but oncei log out it should not be accessed and it should happen by using cookies
thanks in advance
 
here is my code..
 
login.aspx page
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Admin_Loogin.aspx.cs" Inherits="admin_Admin_Loogin" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. </head>  
  7. <body>  
  8. <form id="form1" runat="server">  
  9. <div>  
  10. <div>  
  11. <table style="width: 70%;">  
  12. <caption class="style1">  
  13. <strong>Login Form</strong>  
  14. </caption>  
  15. <tr>  
  16. <td class="style2"></td>  
  17. <td></td>  
  18. <td></td>  
  19. </tr>  
  20. <tr>  
  21. <td class="style2">Username:</td>  
  22. <td>  
  23. <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
  24. </td>  
  25. <td>  
  26. <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"  
  27. ControlToValidate="TextBox1" ErrorMessage="Please Enter Your Username"  
  28. ForeColor="Red"></asp:RequiredFieldValidator>  
  29. </td>  
  30. </tr>  
  31. <tr>  
  32. <td class="style2">password:</td>  
  33. <td>  
  34. <asp:TextBox ID="TextBox2" TextMode="Password" runat="server"></asp:TextBox>  
  35. </td>  
  36. <td>  
  37. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"  
  38. ControlToValidate="TextBox2" ErrorMessage="Please Enter Your word"  
  39. ForeColor="Red"></asp:RequiredFieldValidator>  
  40. </td>  
  41. </tr>  
  42. <tr>  
  43. <td class="style2"></td>  
  44. <td></td>  
  45. <td></td>  
  46. </tr>  
  47. <tr>  
  48. <td class="style2"></td>  
  49. <td>  
  50. <asp:Button ID="Button1" runat="server" Text="Log In" OnClick="Button1_Click" />  
  51. </td>  
  52. <td>  
  53. <asp:Label ID="Label1" runat="server"></asp:Label>  
  54. </td>  
  55. </tr>  
  56. </table>  
  57. </div>  
  58. </div>  
  59. </form>  
  60. </body>  
  61. </html>  
cs code for login
  1. using System.Web.UI.WebControls;  
  2. using System.Data;  
  3. using System.Data.SqlClient;  
  4. using System.Configuration;  
  5. //using System.Web.SessionState;  
  6. public partial class admin_Admin_Loogin : System.Web.UI.Page  
  7. {  
  8. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString());  
  9. protected void Page_Load(object sender, EventArgs e)  
  10. {  
  11. }  
  12. protected void Button1_Click(object sender, EventArgs e)  
  13. {  
  14. SqlCommand cmd = new SqlCommand("select * from tb_admin_login where Userid=@username and password=@word", con);  
  15. cmd.Parameters.AddWithValue("@username", TextBox1.Text);  
  16. cmd.Parameters.AddWithValue("@word", TextBox2.Text);  
  17. SqlDataAdapter da = new SqlDataAdapter(cmd);  
  18. DataTable dt = new DataTable();  
  19. da.Fill(dt);  
  20. con.Open();  
  21. int i = cmd.ExecuteNonQuery();  
  22. con.Close();  
  23. if (dt.Rows.Count > 0)  
  24. {  
  25. Session["id"] = TextBox1.Text;  
  26. HttpCookie loginCookie = new HttpCookie("Login");  
  27. //Set the Cookie value.  
  28. loginCookie.Values["Userid"] = TextBox1.Text;  
  29. loginCookie.Values["Password"] = TextBox2.Text;  
  30. loginCookie.Path = Request.ApplicationPath;  
  31. //Set the Expiry date.  
  32. loginCookie.Expires = DateTime.Now.AddDays(1);  
  33. //Add the Cookie to Browser.  
  34. Response.Cookies.Add(loginCookie);  
  35. Response.Redirect("DashBoard.aspx");  
  36. }  
  37. else  
  38. {  
  39. Label1.Text = "Your username and password is incorrect";  
  40. Label1.ForeColor = System.Drawing.Color.Red;  
  41. }  
  42. }  
  43. }  
master page :
aspx
  1. <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="admin_MasterPage" %>  
  2. <!DOCTYPE html>  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5. <title></title>  
  6. <asp:ContentPlaceHolder ID="head" runat="server">  
  7. </asp:ContentPlaceHolder>  
  8. </head>  
  9. <body>  
  10. <form id="form1" runat="server">  
  11. <div>  
  12. <p>  
  13. <asp:Label ID="Label1" runat="server"></asp:Label>  
  14. </p>  
  15. <p>  
  16. <asp:Button ID="Button1" runat="server" Height="47px" OnClick="Button1_Click"  
  17. Text="Logout" />  
  18. </p>  
  19. <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">  
  20. </asp:ContentPlaceHolder>  
  21. </div>  
  22. </form>  
  23. </body>  
  24. </html>  
master cs page
  1. using System.Web.UI;  
  2. using System.Web.UI.WebControls;  
  3. using System.Data;  
  4. using System.Data.SqlClient;  
  5. using System.Configuration;  
  6. using System.Web.Security;  
  7. public partial class admin_MasterPage : System.Web.UI.MasterPage  
  8. {  
  9. protected void Page_Load(object sender, EventArgs e)  
  10. {  
  11. if (!this.Page.User.Identity.IsAuthenticated)  
  12. {  
  13. FormsAuthentication.RedirectToLoginPage();  
  14. }  
  15. Response.Cache.SetCacheability(HttpCacheability.NoCache);  
  16. Response.Cookies["Userid"].Expires = DateTime.Now.AddDays(-1);  
  17. Response.Cache.SetNoStore();  
  18. HttpCookie loginCookie = Request.Cookies["Login"];  
  19. if (loginCookie != null)  
  20. {  
  21. string name = loginCookie.Values["Name"];  
  22. string password = loginCookie.Values["Password"];  
  23. // User validation based on cookies if required for valid user as might be user can change his password in any cases  
  24. // so call your databse user login validation method  
  25. //if //(name.ToLower() == "sa" && password.ToLower() == "sa") // user validation code  
  26. Label1.Text = (string)Session["id"];  
  27. }  
  28. else  
  29. {  
  30. Response.Redirect("Admin_Loogin.aspx");  
  31. }  
  32. }  
  33. protected void Button1_Click(object sender, EventArgs e)  
  34. {  
  35. Response.Cookies["Userid"].Expires = DateTime.Now.AddDays(-1);  
  36. Response.Redirect("Admin_Loogin.aspx");  
  37. }  
  38. }  

Answers (2)