使用AngularJS的MVC应用程序的Asp.net身份共享身份验证cookie
我正在使用ASP.NETMVC和AngularJs。 我需要认证 我使用VS2015中MVC项目的默认代码进行身份验证。 以下是观点:使用AngularJS的MVC应用程序的Asp.net身份共享身份验证cookie,angularjs,asp.net-mvc,cookies,angular-resource,Angularjs,Asp.net Mvc,Cookies,Angular Resource,我正在使用ASP.NETMVC和AngularJs。 我需要认证 我使用VS2015中MVC项目的默认代码进行身份验证。 以下是观点: <div class="credential_box"> @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" })) { @Html.
<div class="credential_box">
@using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { role = "form" }))
{
@Html.LabelFor(m => m.Username)
@Html.TextBoxFor(m => m.Username, new { @class = "credential" })
@Html.LabelFor(m => m.Password)
@Html.PasswordFor(m => m.Password, new { @class = "credential" })
<input type="submit" value="ACCEDI" style="cursor:pointer" />
}
</div>
还有这样一个控制器:
intranet.controller("countriesController", function ($scope, countriesService, dialogsService) {
$scope.countries = [];
$scope.country = {};
$scope.oldCountryInUpdate = {};
/****************** GET COUNTRIES ******************/
$scope.getCountries = function () {
new countriesService
.countries
.query({ }, getCountriesSuccess, getCountriesError);
};
var getCountriesSuccess = function (data) {
$scope.countries = data;
$scope.country.id = $scope.countries[0].id; //Seleziona il primo elemento della lista
};
var getCountriesError = function (response) { };
}
这是大约3天,我正在尝试,但我不能单独解决这个问题。有人能帮我吗
谢谢
如果我从一个页面转到另一个需要身份验证的页面,我需要登录。
MVC和Web API都通过相同的OWIN cookie中间件。当您登录MVC控制器时,只要它们在同一个域中,您就不需要登录WebAPI控制器。我已经更正了这篇文章。我不太清楚。但是,我在服务器端没有任何问题。问题是当我传递给客户时。当我通过AngularJs提出请求时,我丢失了cookie!如何在AngularJs请求中添加身份验证cookie?如果MVC和Web API都在同一个域中,cookie不会丢失。它们在两个不同的领域吗?MVC和WebApi是的。。。但我认为angularJs和WebApi不。。。
public void ConfigureAuth(IAppBuilder app)
{
...
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
}
intranet
.factory("countriesService", function ($resource, ENDPOINT) {
return {
countries: $resource(ENDPOINT + "api/Countries", null, {
'update' : { method: 'PUT' }
})
};
});
intranet.controller("countriesController", function ($scope, countriesService, dialogsService) {
$scope.countries = [];
$scope.country = {};
$scope.oldCountryInUpdate = {};
/****************** GET COUNTRIES ******************/
$scope.getCountries = function () {
new countriesService
.countries
.query({ }, getCountriesSuccess, getCountriesError);
};
var getCountriesSuccess = function (data) {
$scope.countries = data;
$scope.country.id = $scope.countries[0].id; //Seleziona il primo elemento della lista
};
var getCountriesError = function (response) { };
}