How to intercept an authentication request in an ASP.net web form

I have a user who loses their data because they sit on the page for too long and then ask to log in. I want to do the following:

1) Instead of redirecting them to the login page, I want to cancel the current request and give the user a dialog box with a pop-up login window.

2) When the login is successful, I want the user to be sent back to his form, while all the data remains intact. (Better yet, if the request can pass without sending them back to this form, but this is optional).

How can I intercept these authentication requests and present a popup to the user?

I use ASP.net form authentication.

+3
source share
2 answers

You can catch this event on Application_AuthenticateRequest in Global.asax

But you need to be more specific, are you using ASP.NET Form Authentication?

Added:

Try it and answer me

At Global.asax

void Application_AuthenticateRequest(object sender, EventArgs e)
{

    if (HttpContext.Current.User == null)
    {
        FormsAuthenticationTicket ticket = new
                        FormsAuthenticationTicket(1, "Anonymous", DateTime.Now, DateTime.Now.AddMinutes(30), false, "Anonymous");

        string encryptedTicket = FormsAuthentication.Encrypt(ticket);

        HttpCookie cookie =
           new HttpCookie(FormsAuthentication.FormsCookieName, encryptedTicket);

        Response.Cookies.Add(cookie);

        FormsIdentity id = new FormsIdentity(ticket);

        System.Security.Principal.GenericPrincipal principal = new System.Security.Principal.GenericPrincipal(id, ticket.UserData.Split(new char[] { '|' }));

        Context.User = principal;
    }

}

In web form

string cookieName = FormsAuthentication.FormsCookieName;

    HttpCookie authCookie = Context.Request.Cookies[cookieName];

    FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

    if (authTicket.UserData == "Anonymous")
    {

        //Throw the login popup

    }
    else
    {

        //Some Code

    }
+2
source

Do you use the homepage? You can redirect there when login is required, and not a separate login page. In the login code on the main page, you decide whether to redirect to your own stand-alone login page or make it visible as a login pop-up window.

0
source

Source: https://habr.com/ru/post/1777714/


All Articles