我有一个登录页面,该页面也通过使用 Angular 的 View 进行调用。
一旦它点击“RedirecToLocal”命令..我单步执行,我看到它点击了我的主 Controller 并返回索引 View 就好了..但是当涉及到我的浏览器实际将页面更改为索引时,它就赢了没有改变并且在登录屏幕上保持糟糕。我不明白为什么。我要疯了
账户 Controller
[Authorize]
[InitializeSimpleMembership]
public class AccountController : Controller
{
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginFormModel form, string selectedShow)
{
if (ModelState.IsValid && WebSecurity.Login(form.userName, form.password))
{
...code here...
}
return RedirectToLocal("/"); //HITS HERE SUCCESSFULLY
家庭 Controller
[Authorize]
public class HomeController : Controller
{
public ActionResult Index()
{
return View(); //HITS HERE SUCCESSFULLY
}
}
编辑:
HTML 和 Angular
<button id="loginBtn" ng-click="login()" ng-class="{ 'disabled': isLoading }" class="btn btn-large btn-primary btn-block">
<span>{{buttonText}}</span>
</button>
Angular 登录 Controller :
AccountFactory.login($rootScope.formData);
帐户工厂
AccountFactory.login = function (formData) {
return $http({
method: 'POST',
url: '/Account/Login',
data: formData
});
};
最佳答案
当通过 $http 使用 Ajax 时,出于安全考虑,浏览器不会接受服务器发出的重定向。
使用 ajax 时,处理指示授权的成功返回并通过客户端路由(您喜欢的风格)进行重定向。
AccountFactory.login($rootScope.formData).then(function (data) {
$location.path("/");
});
由于 .then()
被视为成功回调,因此可接受的模式是从 API 返回成功的 200
状态代码以表示成功,并返回 40x
授权尝试失败。
[HttpPost]
[AllowAnonymous]
public ActionResult Login(LoginFormModel form, string selectedShow)
{
if (ModelState.IsValid && WebSecurity.Login(form.userName, form.password))
{
...code here...
return new HttpStatusCodeResult(200);
}
else
{
return new HttpStatusCodeResult(403);
}
}
关于javascript - 重定向到本地不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32961316/