如何在使用MVC身份框架时将值插入数据库中的AspNetUsers表格?

huangapple go评论80阅读模式
英文:

How to insert a value in Database when using MVC identity framework in AspNetUsers Table?

问题

以下是您要翻译的内容:

我想在数据库中插入一个值,我正在使用Asp.Net Identity框架,在我的注册方法中,当一个新用户注册时,我还想添加一个新的Id,这个Id默认情况下不会被Identity框架插入?

if (User.IsInRole("Avanceon")) 
{
    var MaxCustomerId = db.AspNetUsers.Max(x => x.Customer_Id);
    if (ModelState.IsValid)
    {   
        var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            // 在这里为用户分配角色
            await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);

            // 结束
            // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            ViewBag.userCreated = user.UserName + " 创建成功";
            ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
            MaxCustomerId = MaxCustomerId + 1;
            // db.AspNetUsers.LastOrDefault().Customer_Id = MaxCustomerId;
            db.SaveChanges();
            return View();
            //return RedirectToAction("Index", "Home");
        }

        AddErrors(result);
    }
}

ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View(model);
英文:

I want to insert a value in DB and I am using the Asp.Net Identity framework, in my Register method when a new user registers I also want to add a new Id which is client_id by default this id is not inserted by the identity framework?

if (User.IsInRole("Avanceon")) 
{
    var MaxCustomerId = db.AspNetUsers.Max(x => x.Customer_Id);
    if (ModelState.IsValid)
    {   
        var user = new ApplicationUser { UserName = model.UserName, Email = model.Email };
        var result = await UserManager.CreateAsync(user, model.Password);
        if (result.Succeeded)
        {
            // Assign Role to user Here 
            await this.UserManager.AddToRoleAsync(user.Id, model.RoleName);

            // Ends Here
            // await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);
            ViewBag.userCreated = user.UserName + " Created Successfully";
            ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
            MaxCustomerId = MaxCustomerId + 1;
            // db.AspNetUsers.LastOrDefault().Customer_Id = MaxCustomerId;
            db.SaveChanges();
            return View();
            //return RedirectToAction("Index", "Home");
        }

        AddErrors(result);
    }
}

ViewBag.Name = new SelectList(context.Roles.ToList(), "Name", "Name");
return View(model);

答案1

得分: 2

你通常不能直接向AspNetUsers表中插入任何数据。您需要创建一个类,继承自IdentityUser,并在该类中进行必要的更改。

使用代码优先方法,您应该创建一个名为ApplicationUser的类,它继承自IdentityUser,并在其中添加属性:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string ClientID { get; set; }
}

然后在您的代码中目标ApplicationUser(而不是IdentityUser):

var user = new ApplicationUser
{
    UserName = model.UserName,
    Email = model.Email,
    ClientID = model.ClientID,
};

还要进行以下更改:

Startup.csConfigureServices方法中:

services.AddIdentity<ApplicationUser, IdentityRole>();

AccountController中:

private readonly UserManager<ApplicationUser> _userManager;
private readonly SignInManager<ApplicationUser> _signInManager;

为此更改添加迁移以生效。

英文:

You normally can't simply insert any data to AspNetUsers table. You'll need create a class and inherit it from IdentityUser and do the necessary changes in your class.

With code first you should create a class say ApplicationUser which inherits IdentityUser. And add the property there:

public class ApplicationUser : IdentityUser
{
    [Required]
    public string ClientID { get; set; }
}

And then target ApplicationUser (instead of IdentityUser) in your code:

var user = new ApplicationUser
{
    UserName = model.UserName,
    Email = model.Email,
    ClientID = model.ClientID,
};

Make below changes as well:

In ConfigureServices method of Startup.cs

services.AddIdentity&lt;ApplicationUser, IdentityRole&gt;();

In AccountController,

private readonly UserManager&lt;ApplicationUser&gt; _userManager;
private readonly SignInManager&lt;ApplicationUser&gt; _signInManager;

Add migration for this change to take effect

huangapple
  • 本文由 发表于 2020年1月6日 17:07:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定