开发者

How to intercept an authentication request in ASP.net webform

I have user's who are losing their data because they sit on a page too long, then are asked to log back in. I want to do the following:

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

2) When the login is successful, I want the user to be sent back to their form, with all data intact. (Even better if the request could go through without sending them back to that form, but this is optional).

How can I int开发者_开发技巧ercept these authentication requests, and present the user with a popup login?

I am using ASP.net forms authentication.


You can intercept this event on Application_AuthenticateRequest in Global.asax

But, you need be more specific, are you using the ASP.NET Forms Authentication?

Added:

Try this and reply me

In 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 a 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

    }


Are you using a master page? You could redirect to there when login is required, not a separate login page. In the login code in the master page you then decided whether to redirect to a proper, standalone login page, or make a login div visible as a popup.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜