I have a custom AuthorizeAttribute defined in which when the user is unauthorized I am setting a tempdata["UnAuthorized"]=true. I am trying to access this value in an external javascript file which is referenced in the cshtml view, but I am uanble to get the value, it errs out Below is the custom authorize piece
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
filterContext.Result = new RedirectResult("/");
base.HandleUnauthorizedRequest(filterContext);
if (!filterContext.HttpContext.User.Identity.IsAuthenticated)
{
//if not logged, it will work as normal Authorize and redirect to the Login
base.HandleUnauthorizedRequest(filterContext);
}
else
{
filterContext.Controller.TempData ["UnAuthorized"] = true;
filterContext.Result = new RedirectResult("/Error");
}
}
This is how I am trying to access it in my external .js file
function SetData(data) {
var test = TempData["UnAuthorized"];
if (!test)
{
$('#SetModal').html(data);
$('#SetModal').dialog('open');
}
}
I am unable to retrieve the value stored in TempData. Please suggest. I am using asp.net mvc 5, jquery, C#
<input type="hidden" id='hiddError' value="@TempData["UnAuthorized"]" />and access it in ur external js file like thisvar test = $('#hiddError').val();