Reputation: 21
I want to use attribute added to the IdentityUser default property but icant.
Strtup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<IdentityUser, IdentityRole>(options =>
{
options.Password.RequireDigit = true;
options.Password.RequiredLength = 6;
options.Password.RequireNonAlphanumeric = true;
options.Password.RequireUppercase = true;
options.Password.RequireLowercase = true;
options.User.RequireUniqueEmail = true;
// Lockout settings.
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
options.Lockout.MaxFailedAccessAttempts = 5;
options.Lockout.AllowedForNewUsers = true;
}).AddEntityFrameworkStores<ApplicationDbContext>().AddDefaultTokenProviders();
}
ApplicationDbContext.cs
public partial class ApplicationDbContext : IdentityDbContext<IdentityUser>
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);
}
}
ApplicationUser.cs
public class ApplicationUser : IdentityUser
{
public int Coin { get; set; }
}
in this class , i cant change value of the coin
coin is a Adding attribute in default property of IdentityUser AccountController.cs
public class AccountController : Controller
{
private readonly UserManager<IdentityUser> _userManager;
private readonly SignInManager<IdentityUser> _signManager;
private readonly ApplicationDbContext _db;
private readonly AppSettings _appSettings;
public AccountController(UserManager<IdentityUser> userManager,
SignInManager<IdentityUser> signInManager
,
IOptions<AppSettings> appSettings,
ApplicationDbContext db
//IEmailSender emailsender
)
{
_userManager = userManager;
_signManager = signInManager;
_appSettings = appSettings.Value;
_db = db;
}
[HttpPost("[action]")]
public async Task<IActionResult> Register([FromBody] RegisterViewModel formdata)
{
var errorList = new ErrorLoginReg();
var user = new IdentityUser
{
Email = formdata.Email,
UserName = formdata.UserName,
SecurityStamp = Guid.NewGuid().ToString()
};
var result = await _userManager.CreateAsync(user, formdata.Password);
Dictionary<string, string> styleError = new Dictionary<string, string>();
if (result.Succeeded)
{
await _userManager.AddToRoleAsync(user, "Customer");
var dbUser = _db.Users.Where(a => a.Id == user.Id).First();
dbUser.Coin =1500; //Error
_db.Users.Update(dbUser);
await _db.SaveChangesAsync();
return Ok(new { success=true, username = user.UserName, email = user.Email, status = 1, message = "Registration Successful" });
}
else
{
errorList.success = false;
List<errormodel> deserror = new List<errormodel>();
foreach (var error in result.Errors)
{
styleError.Add(error.Code, error.Description);
ModelState.AddModelError("", error.Description);
var ermo = new errormodel();
ermo.code = error.Code;
ermo.message = error.Description;
deserror.Add(ermo);
}
errorList.error = deserror;
}
return Json(errorList);
}
I don't have access to Coin in AccountController !
Upvotes: 0
Views: 60
Reputation: 3177
I don't have access to Coin in AccountController !
It isn't there because you haven't actually instructed Identity to use your ApplicationUser class instead of your IdentityServer class.
Three things need to happen
public partial class ApplicationDbContext : IdentityDbContext<ApplicationUser>
services.AddIdentity<ApplicationUser, IdentityRole>(options => { .. });
> add-migration add_coin_to_user -c ApplicationDbContext
> update-database -c ApplicationDbContext
Edit: Any services depending on IdentityUser should also take the same TKey as what you used to register the services in startup.
private readonly UserManager<IdentityUser> _userManager;
to private readonly UserManager<ApplicationUser> _userManager;
and so forth for every service that takes the IdentityUser.
Upvotes: 2