I am using ASP.NET MVC and AngularJs. I need to authenticate.
I authenticate using the default code of the MVC Project in VS2015. Here the View:
<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>
And here the controller:
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 identity is configured as follow:
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);
}
The login works, because if I pass from a page to another one that require authentication and i am not logged in, I need to login.
Now, there are some parts of the project that use AngularJs. So I have to pass the authentication cookie to the requests I do with AngularJs. Is it possible?
I am using AngularJs Resource to do the requests. So, I have some services:
intranet
.factory("countriesService", function ($resource, ENDPOINT) {
return {
countries: $resource(ENDPOINT + "api/Countries", null, {
'update' : { method: 'PUT' }
})
};
});
And a controller like this:
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) { };
}
It's about 3 days I am trying but I cannot solve this problem alone. Can anyone help me please?
Thank you