Hello,
I am having 3 pages default.aspx, default1.aspx and default2.aspx
Default.aspx is the home page.
When i click default2.aspx, it must check whether i have logged in, if not it must redirect to a login page(default1.aspx)

Dani AI

Generated

As suggested, store a short identifier (not the password) in Session after you validate the login. To answer : set that Session value on the login page, clear it on logout, and check it on any page that requires authentication. Below are concise, practical patterns you can apply immediately.

Login page (after DB check):

if (IsValidUser(username, password)) {
    // store only a user id or GUID
    Session["UserID"] = user.Id;
    string returnUrl = Request.QueryString["ReturnUrl"];
    if (!string.IsNullOrEmpty(returnUrl))
        Response.Redirect(Server.UrlDecode(returnUrl));
    else
        Response.Redirect("~/default.aspx");
}

Protect a page (default2.aspx):

protected void Page_Load(object sender, EventArgs e) {
    if (Session["UserID"] == null) {
        string login = "~/default1.aspx?ReturnUrl=" + Server.UrlEncode(Request.Url.PathAndQuery);
        Response.Redirect(login);
    }
}

Logout:

Session.Clear();
Session.Abandon();
Response.Redirect("~/default1.aspx");

For a cleaner, safer solution use ASP.NET Forms Authentication and web.config to declare a loginUrl — this gives cookie handling and ReturnUrl support out of the box. Example web.config fragment:

<authentication mode="Forms">
  <forms loginUrl="~/default1.aspx" timeout="30" />
</authentication>

Then on successful login call:

FormsAuthentication.RedirectFromLoginPage(user.Id.ToString(), false);

Notes and cautions: centralize the check (inherit pages from a BasePage or use an HttpModule) so you don't repeat code; do not store sensitive data in Session; be mindful of session timeout; use HTTPS and secure/HttpOnly cookies in production.

Recommended Answers

All 2 Replies

Hello,
I am having 3 pages default.aspx, default1.aspx and default2.aspx
Default.aspx is the home page.
When i click default2.aspx, it must check whether i have logged in, if not it must redirect to a login page(default1.aspx)

You may use session to check your user id

Public Function CheckValidation(ByVal UserId As String) As Boolean
        CheckValidation= False
        If UserId = "" Then            
            Exit Function
        End If
        CheckValidation= True
    End Function

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If  CheckValidation(Session("UserID")) = False Then
            Response.Redirect("../../Others/Sorry.aspx")
            Exit Sub
        End If           
    End Sub

You may use session to check your user id

Public Function CheckValidation(ByVal UserId As String) As Boolean
        CheckValidation= False
        If UserId = "" Then            
            Exit Function
        End If
        CheckValidation= True
    End Function

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If  CheckValidation(Session("UserID")) = False Then
            Response.Redirect("../../Others/Sorry.aspx")
            Exit Sub
        End If           
    End Sub

Hello,
I am not using any user controls such as login control. I simply made a login with simple codings. By comparing the textbox values with database the user will log in. Now how the UserID will work. How to do this?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.