Tuesday 15 July 2014

asp.net mvc - How can I authenticate the user within Json controller action? -


I have a .NET MVC controller operation that returns a JsonResult to a YUI AsyncRequest callback. All are working fine with the AsyncRequest call and the "content" model data has been updated successfully. How can I now assure that the user is logged on before AsyncRequest is done?

Normally I will use the [authorized] attribute which gives an error to my YUI AsyncRequest back, as it is expected of a JSON result.

I also tried to check wihtin "User.Identity.IsAuthenticated", but still do not love .. RedirectToRoute is doing nothing.

I have been able to send a JSON as a result of the JS indicating that the user needs to login, but I want to redirect the user to the logon viewer. Here is the action of the Administrator:

  [JsonFilter (Ultimate = "Content"), JsonDataType = typeof [content] public ActionResult SaveJson {content content} {if ( ! User.Identity.IsAuthenticated) RedirectToRoute (new {action = "logon", controller = "account"}); ContentRepository.Update (content); Jason Returns (new {result = "success"}); }  

TIA

You can do something like: [JsonFilter (Ultimate = "Content"), JsonDataType = typeof (content)] Public ActionResult SaveJson (content content) {if (! User.Identity.IsAuthenticated) {var urlHelper = new urlHelper (ControllerContext.RequestContext); Return Jason (new {result = "unconfirmed", url = url halper. Action ("logon", "account")}); } ContentRepository.Update (content); Jason Returns (new {result = "success"}); }

You will use the urlHelper.Action ("logon", "account") part of the result to change the location in the login page (window.location.href = ...).

Additional note: You can move urlHelper.Action ("logon", "account") into your view, as part of your callback function.


No comments:

Post a Comment