Redirect to Page to re-authenticate from Widget Controller

Posted by cschnick@premier-us.net on 25-Jun-2019 19:28

I have a Sitefinity 11 implementation where we have a page that has several custom widgets on it.  Each of these widgets gets data from an application data source (not sitefinity).  If a user sits on this page a LONG time such as locking their computer and going to lunch, the session effectively times out.  If the user clicks on a button on one of the widgets that would trigger an action to get data from the data source for that controller for that widget, we can detect that the session is no longer valid by looking at the LoggedInUser  as such:

readonly Telerik.Sitefinity.Security.SitefinityIdentity LoggedInUser = Security.GetCurrentUserIdentity();

With this LoggedInUser, we can ask:

if (LoggedInUser.IsAuthenticated) {

//.... do something normal like get the data they need

} else {

//.... we would like to redirect back to the page this widget is hosted on to force login

}

What we would like to do is handle the other side of that if statement by redirecting the user back to the page this widget is located on which would trigger the login process and would redirect back to this page after the user logged back in.

How do we trigger the redirect?

I have tried:

var redirectURL = "/myRoot/myPage";
Response.Redirect(redirectURL);

Which seems to cause the widget to disappear off the page but does not result in a redirection.  I have tried substituting a URL to a different page on the site and get the same result.

Thanks,

Chris

All Replies

Posted by cschnick@premier-us.net on 27-Jun-2019 16:15

We have resolved our situation by taking a slightly different approach.  We added a method to the controller that simply checks if the user is still authenticated and returns a True or a False to the JavaScript request.  On the success side of the request, we determine whether a true or false was returned and act accordingly.  If true, we get the data that was needed as was being returned by the original controller action.  If false is returned, from the JavaScript, we simply set the document. location to the page that is hosting the custom control(s) which require(s) authentication.  Since the user is not authenticated at this point, the login process is invoked automatically with a response redirect back to the page we specified as the document.location hosting the controls.

The Controller method is something like...

       readonly Telerik.Sitefinity.Security.SitefinityIdentity LoggedInUser = ClaimsManager.GetCurrentIdentity();

       [HttpGet, Route("web-interface/userIsAuthenticated")]

       public JsonResult ChkUserIsAuthenticated([DataSourceRequest] DataSourceRequest request)

       {

           var userIsAuthenticated = new UserIsAuthenticated();

           if ((LoggedInUser.IsAuthenticated) && (LoggedInUser.UserId != Guid.Parse("00000000-0000-0000-0000-000000000000")))

           {

               userIsAuthenticated.IsAuthenticated = true;

           }

            return Json(JsonConvert.SerializeObject(userIsAuthenticated);, JsonRequestBehavior.AllowGet);

       }

The object returned is defined as....

   public class UserIsAuthenticated

   {

       public bool IsAuthenticated { get; set; }

       public UserIsAuthenticated()

       {

           IsAuthenticated = false;

       }

   }

The JavaScript to call it and handle the response is something like...

   function CheckAuthenticated() {

       // We have to validate that the user is still authenticated before we refresh the data.

       // The asynchronous nature of all this means that the call to check authentication then has

       // handle what to do if the user is or is not authenticated on a success.  In this case the

       // success scenario is a call to another method

       var url = '@Url.Content("/web-interface/UserIsAuthenticated")';

       $.ajax({

           type: "GET",

           url: url,

           contentType: "application/json",

           dataType: "json"

       }).done(function (response) {

           HandleIsAuthenticated(response)

           }).fail(function (response) {

               console.log('Error: ' + JSON.stringify(response));

           });

   }

   function HandleIsAuthenticated(data) {

       var UserIsAuthenticated = JSON.parse(data);

       var userIsAuthenticated = UserIsAuthenticated.IsAuthenticated;

       if (userIsAuthenticated) {

   var someKeyValue= $("#someControl").data("kendoMultiSelect");

           //Get the Data for the custom control

           GetData(someKeyValue.value);

       } else {

           document.location = "/myPageRoot/myPage";

       }

   }

This thread is closed