What Type (class) do i use for a property in my POCO for Asp.net Identy User - ef-code-first

I have a codefirst POCO, and i want to specify a LastEditUser from my current ASP.NET IDENTITY user. I tried type ApplicationUser that gets generated with the new project. but it just saves as null.
Here is my current Attempt
public class SomeClass
{
public string SomeProperty { get; set; }
public ApplicationUser LastEditMember { get; set; }
}
And here is how i try to save it in my controller.
string currentUserId = User.Identity.GetUserId();
ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);
instannceOfSomeClass.LastEditMember = currentUser;
After loading this again. the LastEditMember property is null.

I would suggest using the UserManager class to get the user object instead of working with the DbContext object. You can use UserManager.FindByIdAsync(currentUserId) to get the user. Also I am considering that this code is hit only after a user logs into the application else the currentUserId will be null

Related

How to get Identity User outside Razor Pages in Blazor Server-side?

I am working on a Blazor Server-Side application, using Microsoft Identity, Entity Framework and a multitenant approach with shared Db.
I have extended the IdentityUser class so that I could have the TenantId in the AspNetUser Table
public class ApplicationUser : IdentityUser
{
public int TenantId { get; set; }
}
}
Then I have applied a general query filter to my dbModel based on the TenantId
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Employee>().HasQueryFilter(a => a.TenantId == TenantId);
}
In my blazor page I can call this function
public async Task SetTenant()
{
var authState = await AuthenticationStateProvider.GetAuthenticationStateAsync();
var user = authState.User;
ApplicationUser = await UserManager.FindByNameAsync(user.Identity.Name);
var TenatId = ApplicationUser.TenantId;
}
Finally in my service I can get a list of Employees with the right TenantId
public Task<Employee[]> GetEmployees(int TenatntID)
{
using (var ctx = new ProgramDbContext(TenantId))
{
return Task.FromResult(ctx.Employee.Select(d => new Employee
{
Id = d.Id,
TenantId = d.TenantId,
Name= d.Name,
}).ToArray());
}
}
With this approach, everytime I want to call a function to get DB's Data, I need to identity the user and get the TenantId, then call the specific function and pass the tenantID to it.
I would like to know if my approach is completely wrong to implement this type of solution, for example:
Is it possible to add a Singleton service of an ApplicationUser, so that once is is identified after login, i can inject the service in every class where i need the ApplicationUser.TenantId?
Is it possible to identify and authenticate the Application User outside a blazor class? for example a plain C# class? I was able to pass the AuthenticationStateProvider and UserManager in the constructor of my Service class, but I cant await a function inside the constructor to actually get the ApplicationUser object.
public CaronteWebService(AuthenticationStateProvider authenticationStateProvider, UserManager userManager)
{
_AuthenticationStateProvider = authenticationStateProvider;
_userManager = userManager;
}
UserManager<ApplicationUser> _userManager;
public ApplicationUser ApplicationUser { get; set; }
AuthenticationStateProvider _AuthenticationStateProvider { get; set; }

How can I extend ApplicationUser in ASP.NET Identity but with properties that can be null?

I would like to extend Application User. Here's an example:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity>
GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
return userIdentity;
}
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
I tried this but it seems like when I login if any of these properties is set to a null in the database then when the /Token call is made it returns with an error.
Can someone tell me. Do I need to make a change to the way these properties are set here or in another place?
If i understand your question correctly, a simple solution to your problem could be to create a nullable type. Can you please share what you would like to do? Create a custom identity provider, for example?

Are there any implementations of ASP.NET Identitity that have another level above account?

I am using ASP.NET Identity. It works well but I would like to add in a parent to the AspNetUsers table. In my case I would like to have each user belong to an organization. At this point I am just looking for some ideas to see if others have seen implementations that would allow this.
Has anyone seen any implementations that do this. I would like to get some tips on how I could implement this functionality.
I'm presuming you are using default EF implementation of Identity storage.
Identity is very flexible and can be bent into many shapes to suit your needs.
If you are looking for a simple parent-child relationship, where every user would have a parent record (such as Company), one of the ways to implement that is to add company reference to user class:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.AspNet.Identity.EntityFramework;
public class ApplicationUser : IdentityUser
{
public ApplicationUser()
{
}
[ForeignKey("CompanyId")]
public Company Company { get; set; }
public int CompanyId { get; set; }
}
public class Company
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
public String Name { get; set; }
public virtual ICollection<ApplicationUser> Users { get; set; }
}
This will put a foreign key on users to companies. But from here next course of action depends on what is required in your application. I would imagine that you'll have some sort of restriction for users depending on what company they belong to. For quick company retrieval you can store CompanyId in a claim when logging in the user.
Default implementation of ApplicationUser has GenerateUserIdentityAsync method. You can modify this as following:
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
identity.AddClaim(new Claim("CompanyId", CompanyId.ToString()));
return userIdentity;
}
Then on every request you'll be able to access this CompanyId claim from the cookie:
public static int GetCompanyId(this IPrincipal principal)
{
var claimsPrincipal = principal as ClaimsPrincipal;
//TODO check if claims principal is not null
var companyIdString = claimsPrincipal.Claims.FirstOrDefault(c => c.Type == "CompanyId");
//TODO check if the string is not null
var companyId = int.Parse(companyIdString); //TODO this possibly can explode. Do some validation
return companyId;
}
And then you'll be able to call this extension method from almost anywhere of your web application: HttpContext.Current.User.GetCompanyId()

Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type ApplicationUser

I am stuck at an odd issue.
I am learning MVC 5 and almost everything is created by the built-in template.
I only added a class History
public class History
{
[Key]
public DateTime Date { get; set; }
public virtual ApplicationUser User { get; set; }
public string ItemName { get; set; }
}
And inside the built-in ApplicationUser:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
public virtual ICollection<History> Histories { get; set; }
}
Here is the error message:
Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Multiple object sets per type are not supported. The object sets 'ApplicationUsers' and 'Users' can both contain instances of type 'MyOnlineShopping.Models.ApplicationUser'.
Source Error:
Line 124: {
Line 125: var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
Line 126: IdentityResult result = await UserManager.CreateAsync(user, model.Password);
Line 127: if (result.Succeeded)
Line 128: {
Source File: f:\Workplace\MyOnlineShopping\MyOnlineShopping\Controllers\AccountController.cs Line: 126
The above is just a work around, There are many answers to this same problem all around SO.
Here
Here
and Here
Look inside IdentityModel.cs, you will find
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
Inside of this context, VS sometimes adds DbSet<ApplicationUser> ApplicationUsers
THIS IS A DUPLICATE
Inside of the IdentityDbContext class is a DbSet User, when you subclass from IdentityContext, inheritance converts that line to DbSet<ApplicationUser> User.
The fix? Remove the DbSet<ApplicationUser> User from public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
This error happens when your ApplicationDbContext class changed. just do this thing into your Models Folder and go to the IdentityModels.cs class and remove the Last line
public System.Data.Entity.DbSet < Project.Models.ApplicationUserProject.Models.ApplicationUser> ApplicationUsers { get; set; }
This line is generated automatically by scaffold.
I ended up renaming ApplicationUser to User, rescaffolding and everything magically started working.
Looks like the templates are a bit out of wack.

Edit model with virtual field

I have a Shop model which contains several fields. One of which is a virtual User one. Whenever I try to edit one entry I get an error saying that User field is required.
public class Shop
{
//..
public virtual ApplicationUser User { get; set; }
//..
}
My workaround is this:
shop.User = shop.User; //re-set the value
shop.Active = true;
db.Entry(restaurant).State = EntityState.Modified;
db.SaveChanges();
And I have to do this for all the fields. Is this the standard approach for this or is there a better way?
Change your model to this:
public class Shop
{
//..
public int UserId {get; set; }
public virtual ApplicationUser User { get; set; }
//..
}
Entity Framework will automatically detect that UserId is the foreign key for object User. You had this problem because User is virtual (lazy loaded). When changing the model without accessing or setting this property EF thinks it's empty (I assume). The foreign key UserId is not virtual, and will be fetched together with the other properties of model Shop, so you don't have to re-set the value when saving the model.
To set a new user, you now have to do for example:
myShop.UserId = 1; // instead of setting myShop.User
For more information, see this article: http://msdn.microsoft.com/en-us/data/jj713564.aspx

Resources