在Asp,NET MVC中使用Javascript访问受保护的页面返回未经授权的结果
Accessing Protected Page Using Javascript in Asp,NET MVC Returns Unauthorized
我正试图使用javascript将用户重定向到控制器操作,但我一直得到未经授权的响应
Java脚本代码如下
var instance = axios.create({
baseURL: this.baseUrl, headers: {'X-Requested-With': 'XMLHttpRequest'},
});
instance.post('Account/Login', d)
.then(function(resonse){
console.log(resonse);
window.location = this.baseUrl + 'live';
}).catch(function(response){
console.log(response);
window.location.href = this.baseUrl +'Account/Login';
}.bind(this));
我的登录例程实现如下
if (HttpContext.Request.IsAjaxRequest())
{
if (!ModelState.IsValid)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest,
"A valid Email address and password is required");
}
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
{
return RedirectToAction("Index", "Live");
}
case SignInStatus.LockedOut:
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Account is disabled at the moment");
case SignInStatus.RequiresVerification:
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Email address requires confirmation");
case SignInStatus.Failure:
default:
return new HttpStatusCodeResult(HttpStatusCode.Unauthorized, "Invalid Log in Attempt");
}
}
我试图重定向到的操作方法如下
[Authorize]
public class LiveController : Controller
{
[Route("live")]
public ActionResult Index()
{
return View();
}
}
现在我的问题是,当身份验证成功并且我试图重定向到/live url时,即使身份验证成功,我仍然会收到服务器的未经授权的响应。
确保正在设置.ASPXAUTH
(或自定义名称时所称的任何名称)cookie。这真的是唯一会让你在验证后看起来未经验证的东西。AJAX请求应该支持服务器设置cookie,但可能有一些奇怪的东西在起作用。
也就是说,如果你要做的只是在之后立即重定向,那么通过AJAX登录真的没有意义。你可以很容易地用returnUrl
做一个标准的帖子,并达到同样的效果,这肯定会在100%的时间内起作用。
相关文章:
- JSONP请求返回结果,但也触发error_callback
- async问题,JS Promise无法返回结果,但可以使用console.log
- Meteor js-控制台显示'未定义'即使返回结果
- 正在等待HTTP调用从函数返回结果
- 使用函数for循环遍历对象以更改值,然后返回结果
- 迭代 JSON 文件并且未在 Javascript 中正确返回结果
- Javascript 函数不返回结果
- 用逗号分隔返回结果中的对象
- 需要呈现MongoDB查询返回结果的特定索引/位置
- HTML5 文件阅读器如何返回结果
- javascript循环遍历列表项,并在数组中返回结果
- 使用jquery UI next按钮提交输入字段值,php将返回结果
- 在angularjs工厂中,如何在函数解析后返回结果
- 我在jQuery中的Rock Paper Scissors程序只在平局时返回结果
- 如何使用ajax调用php并返回结果
- 添加第二维度的Javascript未返回结果
- Node.js没有返回结果
- jquery裁剪图像selction未返回结果
- 如何在phonegap javascript函数中返回结果
- 如果文本筛选器在ng重复中未返回结果,则显示消息