3

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#

8
  • it should be var test = '@TempData["UnAuthorized"]'; Commented Jul 20, 2015 at 22:05
  • I tried this and it just displays the '@TempData["UnAuthorized"]' as a string and still not displaying the value. It is displaying the string with "/" "@TempData[\"UnAuthorized\"]" Commented Jul 20, 2015 at 22:23
  • are you using this in an external js file? if yes then try it in a .cshtml file Commented Jul 20, 2015 at 22:31
  • Yes Sushil, I am using external js file Commented Jul 20, 2015 at 22:31
  • then you wont be able to access the C# code. what you can do is add the value of TempData in a hiddenField in your .cshtml page and use the value of it in the external js file. e.g. <input type="hidden" id='hiddError' value="@TempData["UnAuthorized"]" /> and access it in ur external js file like this var test = $('#hiddError').val(); Commented Jul 20, 2015 at 22:36

1 Answer 1

7

Put another script in your razor view to store the value in a javascript variable and then you can use the value in your external file.

Razor:

@section scripts {
    <script type="text/javascript">
        var unauthorized = '@TempData["UnAuthorized"]';
    </script>
    <script type="text/javascript" src="~/Content/Scripts/external.js">
}

External.js

if(unauthorized) {
    alert("unauthorized");
}
Sign up to request clarification or add additional context in comments.

1 Comment

I have master.cshtml which refs master.js. Master.cshtml has a custom context menu,when clicked posts a ajax request which calls a controller/action method which has customauthorize attribute. Since the user is not authorized customauthorize attribute redirects to unauthorized.cshtml where I put your script code unauthorized="@Tempdata['UnAuthorized']". same master.js file in this cshtml as well. SetData code is in master.js and var unauthorized=undefined. The flow is master.cshtml--->customattribute--fails to atuhroize since the user does not have rights-->unauthorized.cshtml--->master.js

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.