我正在使用ASP.NET MVC和AngularJs。 我需要进行身份验证。
我使用VS2015中MVC项目的默认代码进行身份验证。 在这里查看:
<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>
这里是控制器:
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: true);
switch (result)
{
case SignInStatus.Success:
return RedirectToLocal(returnUrl);
case ...
}
}
Asp.Net标识配置如下:
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);
}
登录有效,因为如果我从一个页面传递到另一个需要身份验证并且我没有登录的页面,我需要登录。
现在,项目的某些部分使用AngularJs。所以我必须将身份验证cookie传递给我使用AngularJs的请求。有可能吗?
我正在使用AngularJs Resource来执行请求。所以,我有一些服务:
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) { };
}
我正在尝试大约3天,但我不能单独解决这个问题。有人可以帮帮我吗?
谢谢