I am making an MVC/AngularJs SPA with Entity Framework. I am saving a vehicle to my database and trying to also save the current logged in user by UserId as a foreign key, but the result is always null. I am using Identity for authorization and authentication.
VehicleController.cs (everything in comments is what I tried so far, always null, so I guess the problem might be somewhere else):
[Authorize]
[Route("")]
public async Task<IHttpActionResult> PutVehicle(Vehicle vehicle)
{
//vehicle.UserId = HttpContext.Current.GetOwinContext().GetUserManager<UserModel>().UserId;
//vehicle.UserId = User.Identity.GetUserId();
//vehicle.UserId = Request.GetOwinContext().Authentication.User.Identity.GetUserId();
//vehicle.UserId = HttpContext.Current.GetOwinContext().Authentication.User.Identity.GetUserId();
//vehicle.UserId = RequestContext.Principal.Identity.GetUserId();
//vehicle.UserId = HttpContext.Current.User.Identity.GetUserId();
vehicle.Discontinued = false;
this.Validate(vehicle);
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
_db.Vehicles.Add(vehicle);
await _db.SaveChangesAsync();
return Ok();
}
The Vehicle Model:
public class Vehicle
{
[Key]
public int VehicleId { get; set; }
public string UserId { get; set; }
public string LicenseNumber { get; set; }
public string Company { get; set; }
public bool Discontinued { get; set; }
[JsonIgnore]
public List<Trip> Trips { get; set; }
[JsonIgnore]
public UserModel User { get; set; }
}
VehicleController.js:
vm.addVehicle = function () {
vehicleService.addVehicle(vm.newVehicle).then(function() {
vm.savedSuccessfully = "Your vehicle was added successfully!";
vm.getAllVehicles();
});
}
VehicleService.js:
var _addVehicle = function(vehicle) {
var deferred = $q.defer();
$http.put(serviceBase + 'api/vehicle',
vehicle,
{ headers: { 'Authorization': 'Bearer' + authService.authentication.token } })
.success(function(response) {
deferred.resolve(response);
}).error(function (error, status) {
console.log(status);
console.log(error);
deferred.reject(error);
});
return deferred.promise;
};
I really don't know what I'm looking for anymore.
Edit: Updating with some more stuff that might be relevant to the problem I'm having.
authService.js:
var _login = function(loginData) {
var data = "grant_type=password&username=" + loginData.userName + "&password=" + loginData.password;
var deferred = $q.defer();
$http.post(serviceBase + 'token',
data,
{ headers: { 'Content-Type': 'application/x-www-form-urlencoded' } }).success(function(response) {
localStorageService.set('authorizationData',
{ token: response.access_token, userName: loginData.userName });
_authentication.isAuth = true;
_authentication.userName = loginData.userName;
deferred.resolve(response);
}).error(function(err, status) {
_logOut();
deferred.reject(err);
});
return deferred.promise;
};
authInterceptorService.js
var _request = function(config) {
config.headers = config.headers || {};
var authData = localStorageService.get('authorizationData');
if (authData) {
config.headers.Authorization = 'Bearer ' + authData.token;
}
return config;
}
SimpleAuthorizationServerProvider.cs
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
using (AuthRepository _repo = new AuthRepository())
{
IdentityUser user = await _repo.FindUser(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
}
ClaimsIdentity identity = new ClaimsIdentity(OAuthDefaults.AuthenticationType);
identity.AddClaim(new Claim("sub", context.UserName));
identity.AddClaim(new Claim("role", "user"));
context.Validated(identity);
}
AuthRepository.cs
public async Task<IdentityUser> FindUser(string userName, string password)
{
IdentityUser user = await _userManager.FindAsync(userName, password);
return user;
}