This question already has answers here:
extend ASP.NET Core Identity user
(2 answers)
Closed 3 years ago.
I am a newbie in ASP.NET Core.
How can I add custom fields to my user model both in code and UI?
Current user management UI is something like this, but I want to add some extra fields, like credit or scores, in model, DB & UI:
And this is my AspNetUser model :
public partial class AspNetUsers
{
public AspNetUsers()
{
AspNetUserClaims = new HashSet<AspNetUserClaims>();
AspNetUserLogins = new HashSet<AspNetUserLogins>();
AspNetUserRoles = new HashSet<AspNetUserRoles>();
AspNetUserTokens = new HashSet<AspNetUserTokens>();
}
public string Id { get; set; }
public string UserName { get; set; }
public string NormalizedUserName { get; set; }
public string Email { get; set; }
public string NormalizedEmail { get; set; }
public bool EmailConfirmed { get; set; }
public string PasswordHash { get; set; }
public string SecurityStamp { get; set; }
public string ConcurrencyStamp { get; set; }
public string PhoneNumber { get; set; }
public bool PhoneNumberConfirmed { get; set; }
public bool TwoFactorEnabled { get; set; }
public DateTimeOffset? LockoutEnd { get; set; }
public bool LockoutEnabled { get; set; }
public int AccessFailedCount { get; set; }
public double Credit { get; set; }
public virtual ICollection<AspNetUserClaims> AspNetUserClaims { get; set; }
public virtual ICollection<AspNetUserLogins> AspNetUserLogins { get; set; }
public virtual ICollection<AspNetUserRoles> AspNetUserRoles { get; set; }
public virtual ICollection<AspNetUserTokens> AspNetUserTokens { get; set; }
}
Custom user data is supported by inheriting from IdentityUser. It's customary to name this type ApplicationUser:
public class ApplicationUser : IdentityUser
{
public string CustomTag { get; set; }
}
The detail steps to customize could be found in Customize the model document .
In ASP.NET Core 2.1 or later, Identity is provided as a Razor Class Library. For more information, see Scaffold Identity in ASP.NET Core projects .
After scaffolding , you can find the profile page in Areas.Identity.Pages.Account.Manage.Index.cshtml , you can add entities in InputModel of Index.cshtml.cs and customize the data in OnGetAsync and OnPostAsync function .
Related
I am creating API in ASP .NET Core that will retrieve posts with user Id. Post should contain text and Id of a user who posted it.
I have two models Users and Posts and I need help on how to configure this relationship
I want one User to have many posts
Currently my user model contains
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public List<Post> Posts { get; set; }
}
And my Post model
public class Post
{
public int Id { get; set; }
public string Text { get; set; }
}
What is the best way to do this ?
One to many relationships ( User to have many posts).
public class User{
public int Id { get; set; }
public string Username { get; set; }
public List<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Text { get; set; }
//Navigation
public int UserId { get; set; }
public User User{ get; set; }
}
this is your Model Class:
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public Virtual List<Post> Posts { get; set; }
}
public class Post
{
public int Id { get; set; }
public string Text { get; set; }
public int UserId { get; set; }
public Virtual User User { get; set; }
}
and in your DbContext:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// configures one-to-many relationship
modelBuilder.Entity<User>().HasMany(x=>x.Posts).WithRequired(x=>x.User)
.HasForeignKey<int>(s => s.UserId);
}
I recently added a new model to my MVC application that looks like this
namespace PIC_Program_1._0.Models
{
public class DisposalOrder
{
[Key]
[Display(Name = "DO ID")]
public int ID { get; set; }
[DataType(DataType.Date)]
[DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
[Display(Name = "Date Created")]
public DateTime DateCreated { get; set; }
[DefaultValue("Pending")]
public DOStatus Status { get; private set; }
public decimal disposed { get; set; }
public string Details { get; set; }
public virtual ICollection<DODetails> DisposalDetails { get; set; }
public virtual Site sites { get; set; }
public virtual Component Components { get; set; }
public virtual Item Items { get; set; }
public virtual Part Parts { get; set; }
public bool deleted { get; set; }
public bool hasBeenReleased { get; set; }
public string Notes { get; private set; }
}
}
But when I run a add-migration and an update database, nothing is changed.
I created the model by right-clicking my Models folder, then typing "Add class" so it should be setup the right way. I'm stumped as to why it's not adding?
I have a table named User in my database. I also have a .net core project where authentication is built-in. I want to connect to my database and after scaffolding reveals my User class, I want it to inherit from IdentityUser.
After scaffolding went well, I tried to inherit from IdentityUser.
public partial class User :IdentityUser<int>
{
public int Id { get; set; }
public string Country { get; set; }
public string PersonalId { get; set; }
public string MobilePhone { get; set; }
public string Email { get; set; }
public DateTime BirthDate { get; set; }
public string Password { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public byte[] Idimage { get; set; }
public bool? EmailConfirmed { get; set; }
public bool? Smsconfirmed { get; set; }
}
I can not see Identity Fields like PasswordHash, PhoneNumberConfirmed and so on, in my database. Specifically, in User table.
I am working on an asp.NET MVC4 web application. I have a create view with Upload file (CV). I succeeded to upload the file correctly but I need to show it (the CV) in the Details view and i don't know how to do it.
Here's the class :
public class Consultant
{
public int Id { get; set; }
public int CIN { get; set; }
public string Nom { get; set; }
public string Prenom { get; set; }
public string RefCommercial { get; set; }
[DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
public string DateNaissance { get; set; }
public string Fonction { get; set; }
public string Tel { get; set; }
public string Experience { get; set; }
public string Email { get; set; }
public string CVurl { get; set; }
The attribute CVurl contains the file's name.
Now i want to show the file in the Details view of a consultant.
Any help? Thanks
I have a custom SimpleMembershipProvider and i have defined all tables that the SimpleMembershipProvider requires, like webpages_Membership:
[Table("webpages_Membership")]
public class Membership
{
public Membership()
{
Roles = new List<Role>();
OAuthMemberships = new List<OAuthMembership>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
[StringLength(128)]
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
[Required, StringLength(128)]
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
[Required, StringLength(128)]
public string PasswordSalt { get; set; }
[StringLength(128)]
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
public ICollection<Role> Roles { get; set; }
[ForeignKey("UserId")]
public ICollection<OAuthMembership> OAuthMemberships { get; set; }
}
But how do i add a record in this table within the Seed() method in the configuration.cs file that migrations creates when enabled?
Assuming you have the custom membership provider correctly configured in the web.config you should be able to add a record in the Seed method by doing this.
var membership = (MyMembershipNamespace.SimpleMembershipProvider)Membership.Provider;
membership.CreateUserAndAccount("test", "password");