我必须从API部分分离aspnetboilerplate(Asp.Net MVC + AngularJS前端)Web,并同时在AngularJS + ASP.Net MVC中验证用户。这适用于单个域。当API托管在另一台服务器上时不起作用。通过AJAX登录,在MVC + Angular中使用cookie。 CORS
我的MVC的AccountController动作登录:
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = rememberMe }, identity);
return Json(new AjaxResponse {Success = true, TargetUrl = Request.ApplicationPath });
我Login.js文件:
$("#LoginButton").click(function(e) {
e.preventDefault();
abp.ui.setBusy(
$("#LoginArea"),
abp.ajax({
url: "http://internallyAccesibleServer/Account/Login",
//url: abp.appPath + "Account/Login",
type: "POST",
crossDomain: true,
data: JSON.stringify({
tenancyName: $("#TenancyName").val(),
usernameOrEmailAddress: $("#EmailAddressInput").val(),
password: $("#PasswordInput").val(),
rememberMe: false
})
})
.done(function(response) {
abp.message.info("Login succeeded");})
.fail(function(data) {
abp.message.error(data.details, data.message);
})
);
});
当我在hxxp运行此:// internallyAccesibleServer/- 登录成功,我可以访问授权在AngularJS上复活。 如果我从任何其他服务器运行相同的Login.js代码 - 我看到“登录成功”消息片刻(也在我的WithinAccesibleServer上看到数据库/日志记录 - 所以AJAX请求工作正常),然后HTTP响应302,女巫由MVC框架从401转换而来,位置标题中的URL为“hxxp:// otherserver/account/login” - 因此是请求的来源。
我对CORS的Web.config:
我相信这事做与AJAX和MVC之间的Cookie共享。有人可以帮我解决这个问题吗?或者我应该开始在没有任何MVC Razor视图的情况下以完整的Angular重写GUI,并使用其他认证类型,比如Bearer? 完整的应用程序已经开发使用剃刀视图:-(
我已阅读所有相关的forum.aspnetboilerplate.com消息,但没有答案,这个确切的问题对不起,如果链接将过期 - 添加主题名称为。参考
分布式的WebAPI和MVC SITE hxxps://forum.aspnetboilerplate.com/viewtopic.php F = 2 & T = 871
DEPLOY ABP ON 3 TIER hxxps://forum.aspnetboilerplate .com/viewtopic.php?f = 2 & t = 2376
3-TIER DEPLOYMENT。 SEPARATE GUI从申请 hxxps://forum.aspnetboilerplate.com/viewtopic.php F = 2 & T = 3301
非常类似的问题(但是没有溶液): hxxps://stackoverflow.com/questions/ 43747413 /发送cookie -cross-domain-with-cors-using-jquery-hosted-with-mvc-troubleshoo
我也试过没有运气在.Web层中实现我自己的WebClient,并将一个cookie暴露给后续的AJAX调用。让AJAX调用在浏览器中进行身份验证的最佳方法是什么?
P.S.这是我的OWIN启动配置寻址身份验证:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationMode = AuthenticationMode.Active,
CookieHttpOnly = false,
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
LogoutPath = new PathString("/Account/Logout"),
Provider = new CookieAuthenticationProvider
{
OnApplyRedirect = ctx =>
{
if (!IsAjaxRequest(ctx.Request))
{
ctx.Response.Redirect(ctx.RedirectUri);
}
}
}
});
我会非常感谢任何答案。
+2
基于cookie的认证不工作不管您的CORS设置如何,因为Cookie始终是域绑定的。 –
+0
我这么认为:-(谢谢你的回答。 –