Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
719 views
in Technique[技术] by (71.8m points)

asp.net mvc - How may I best use the [Authorize] attribute with Ajax and Partial Views?

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 expected).

It seems like a long shot; but, is there any way to extend this attribute to "break out" of an AJAX call and redirect the entire page to the login screen? (As opposed to returning the login screen to whatever location I've set UpdateTargetId?)

I think I have a handle on how I'd do this on each of the specific Ajax calls, but if I could centralize this somehow, it'd save a lot of code...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...