0

When user log in my web,I set the cookie's timeout to be 30 min,after timeout,when user want to go to page B from page A,I will check the cookie time out and return the javascript to the brower,what's wrong with my codes?It seems that I return the content because all the codes below will display in the browser but not execute(and I have to refresh the url and is OK).

Method 1:not work

  public override void OnAuthorization(AuthorizationContext filterContext)
   {
      var url = string.Format("{0}?ReturnUrl={1}",
                FormsAuthentication.LoginUrl,
                filterContext.HttpContext.Request.RawUrl);
      if (SessionHelper.Get("UserName") == null)
      filterContext.HttpContext.Response.Write(
       "<script>alert('Login overtime!Loggin again please!');
       window.location.href='"+url +"';</script>");
   }

Method 2:not work

  public override void OnAuthorization(AuthorizationContext filterContext)
   {
      var url = string.Format("{0}?ReturnUrl={1}",
                FormsAuthentication.LoginUrl,
                filterContext.HttpContext.Request.RawUrl);
      if (SessionHelper.Get("UserName") == null)
       filterContext.Result = new ContentResult
        {
         ContentType = "text/html",
         Content = "<script>alert('Login overtime!Loggin again please!');
        window.location.href='" + url + "';<script>"
        };
     }
3
  • Why dont you just use RedirectResult instead of ContentResult? You could pass over the reason as a tempdata. Commented Feb 26, 2014 at 15:51
  • Thanks,Before redirect the url,I want the alert the overtime message.How could I alert the tempdata in OnAuthorization function?Every page I have to verify,I can not pass tempdata in all page or all controller. Commented Feb 26, 2014 at 15:54
  • I'll post a quick answer for you 2 secs Commented Feb 26, 2014 at 16:04

2 Answers 2

1
public override void OnAuthorization(AuthorizationContext filterContext)
    {
        if (SessionHelper.Get("UserName") == null)
        {

            filterContext.Controller.TempData.Add("RedirectReason", "Login overtime!Loggin again please!");
            filterContext.Result = new RedirectResult("~/Login");

        }
    }

Then add this to your view...

@TempData["RedirectReason"]

and the error will be displayed on your login form.

Hope this helps!

Sign up to request clarification or add additional context in comments.

Comments

0

we should not return javascript alerts..instead see what the error code you are getting when you make a service call after the cookie timesout. based on that, add the below to your master page

$.ajaxSetup({
            statusCode: {
                407: function () {
                  //your redirection code here
                 window.location.reload();
                }
            },


        });

//do not use 407 in your code..see what code u get,then replace 407 with ur status code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.