重定向阿贾克斯后采取行动,其他一些actioin [英] Redirect ajax post action to some other actioin
问题描述
我用ajax后的行动。在这次行动中我使用了 HttpContext.User.Identity.Name
来获取用户ID。基于用户ID,我得到与在数据库中,并返回用户的ID通过JSON类型值的一些记录。
有时候会得到过期,在这种情况下, HttpContext.User.Identity.Name
值成空。如果为空或null会抛出异常。
所以,我需要为空或空检查 HttpContext.User.Identity.Name
的价值,如果它是null或空,我需要把它重定向到登录页面。
但重定向动作不是工作的AJAX动作后内。如何解决这个问题呢?
我需要授权阿贾克斯后的行动。任何人能给出的解决方案是什么?
问候,
KARTHIK。
但重定向动作不是工作的AJAX动作后内。如何解决这个问题呢?
您可以通过装饰你的控制器动作与 [授权]
属性开始。这可确保只有合法的用户才能访问它,你保证这里面 User.Identity.Name
永远不会为空:
[授权]
公众的ActionResult SomeAction()
{
字符串的用户名= User.Identity.Name; //这不会抛出
...
返回JSON(...);
}
再看看在<一个href="http://haacked.com/archive/2011/10/04/$p$pvent-forms-authentication-login-page-redirect-when-you-donrsquot-want.aspx"相对=nofollow>下面的博客文章通过菲尔哈克。其内后菲尔presents一个很好的插件,允许您配置ASP.NET发送401 HTTP状态code,当一个Ajax请求保护的诉讼作出。因此,在您的jQuery code,你可以很容易地检测到这种情况,并重定向:
$。阿贾克斯({
网址:'/ SomeAction,
键入:POST,
状态code:{
200:功能(数据){
警报(200:认证');
// JSON数据绑定到用户界面
},
401:功能(数据){
//该用户没有通过验证=&GT;他重定向到登录页面
window.location.href ='/登录;
}
}
});
当然,避免写这401条件所有的AJAX请求,你可以很容易地使用全局 .ajaxError()
处理集中的情况下,由服务器返回401状态code所有的AJAX请求这个重定向逻辑:
$(文件).ajaxError(函数(即jqxhr,设置例外){
如果(jqxhr.status == 401){//未授权
window.location.href ='/登录;
}
});
,现在你的AJAX请求成为pretty的标准:
$。阿贾克斯({
网址:'/ SomeAction,
键入:POST,
成功:功能(数据){
//做一些与动作返回的数据
}
});
I am using ajax post action. In this action i have used HttpContext.User.Identity.Name
to get the user id. Based on the user id i get some records related to that user id in database and return that values through json type.
Sometimes session got expired, in this case the HttpContext.User.Identity.Name
value became empty. If it is empty or null it will throw the exception.
So I need to null or empty check the HttpContext.User.Identity.Name
value, if it is null or empty i need to redirect it to login page.
But redirecting action not works inside the ajax post action. how to solve this problem?
i need to authorize the ajax post action. Can any one give solution for that?
Regards,
Karthik.
But redirecting action not works inside the ajax post action. how to solve this problem?
You could start by decorating your controller action with the [Authorize]
attribute. This ensures that only authenticated users can access it and you are guaranteed that inside User.Identity.Name
will never be null:
[Authorize]
public ActionResult SomeAction()
{
string username = User.Identity.Name; // this will never throw
...
return Json(...);
}
then take a look at the following blog post by Phil Haack. Inside its post Phil presents a nice plugin that allows you to configure ASP.NET to send 401 HTTP status code when an AJAX request to a protected action is made. So in your jQuery code you could very easily detect this condition and redirect:
$.ajax({
url: '/SomeAction',
type: 'POST',
statusCode: {
200: function (data) {
alert('200: Authenticated');
// Bind the JSON data to the UI
},
401: function (data) {
// the user is not authenticated => redirect him to the login page
window.location.href = '/login';
}
}
});
And of course to avoid writing this 401 condition in all your AJAX requests you could very easily use the global .ajaxError()
handler to centralize this redirection logic for all your AJAX requests in case of 401 status code returned by the server:
$(document).ajaxError(function(e, jqxhr, settings, exception) {
if (jqxhr.status == 401) { // unauthorized
window.location.href = '/login';
}
});
and now your AJAX requests become pretty standard:
$.ajax({
url: '/SomeAction',
type: 'POST',
success: function(data) {
// do something with the data returned by the action
}
});
这篇关于重定向阿贾克斯后采取行动,其他一些actioin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!