0

I am trying to use .NET identity. My user has properties which are related to auth. For example, email address, password, projects user has access to, etc. Also, the user has other fields, for example favourite color.

  • Because color is not related to authentication, I don't want to add color as a property of ApplicationUser : IdentityUser {}.

  • But somehow I need to write values into that field, so I could write a repository, which takes the regular User object. But in that case I need to write the repository, hash the password myself, so what's the point having .NET Identity?

  • A third solution would be to make 2 tables, one for the ApplicationUser, and one for the related properties, but we already have a database, and I don't want to change everything.

  • One more idea what I could think of is to have ApplicationUser : IdentityUser {} as basibally a copy of User (I can't inherit from user and IdentityUser too, and can't chain it, because of database first) and have a separate AuthenticationUser : IdentityUser in the auth. context.

How sould I do it?

thanks

barii
  • 343
  • 1
  • 3
  • 12
  • You should separate that (last solution) as favorite color will be rather rarely used comparing to identity and other data. User from identity perspective has different usage context. – jkosmala Sep 27 '18 at 09:02
  • 1
    Please read my answer here: https://stackoverflow.com/questions/52079466/is-claims-based-authorization-appropriate-for-individual-resources/52100609#52100609 –  Oct 03 '18 at 09:23
  • @RuardvanElburg This is a good answer and I upvodet that and comment. But my answer may useful. – AmirReza-Farahlagha Oct 03 '18 at 11:02

1 Answers1

0

I think for this issue you have to change your model like:

public class Person
{
    public string Color { get; set: }
    public int RoleId { get; set: }
    public Role Role { get; set: }

    public ICollection<UserAuthentication> UserAuthentications { get; set: }
}

public class UserAuthentication
{
    public Person Person { get; set: }
    public int PersonId { get; set: }
}

With these models you don't have this problem.

If you have any data, you can displace that using TSql.

user229044
  • 232,980
  • 40
  • 330
  • 338
AmirReza-Farahlagha
  • 1,204
  • 14
  • 26