How may I best use the [Authorize] attribute with Ajax and Partial Views?

前端 未结 1 789
时光说笑
时光说笑 2020-12-15 00:56

I was about to use [Authorize] on Actions that return partial views through Ajax; but I\'m not getting the behavior I\'d like (although its the behavior I expec

相关标签:
1条回答
  • 2020-12-15 01:47

    AuthorizeAttribute won't do this for you, but you can centralize your code. Create a new attribute which returns http status code 401 (not authorized) when no user is currently logged in. In a JavaScript file referenced by your site.master, handle the jQuery $.ajaxError event. Look for http 401 in the response status code and redirect to your login page via JavaScript. Now any Ajax call which you make via jQuery will end up here if the current user is not authorized.

    0 讨论(0)
提交回复
热议问题