访问AccountController外部的UserManager

Spi*_*red 28 asp.net-mvc actioncontroller asp.net-mvc-5 asp.net-identity-2

我试图aspnetuser从不同的控制器(而不是accountcontroller)设置表中的列的值.我一直试图访问,UserManager但我不知道如何做到这一点.

到目前为止,我已经在控制器中尝试了以下内容我想用它:

    ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
    u.IsRegComplete = true;
    UserManager.Update(u);
Run Code Online (Sandbox Code Playgroud)

这不会编译(我想因为UserManager还没有实例化控制器)

我还尝试创建一个公共方法,AccountController以接受我想要更改值的值并在那里做,但我无法弄清楚如何调用它.

public void setIsRegComplete(Boolean setValue)
{
    ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
    u.IsRegComplete = setValue;
    UserManager.Update(u);

    return;
}
Run Code Online (Sandbox Code Playgroud)

如何访问和编辑帐户控制器之外的用户数据?

更新:

我尝试在其他控制器中实例化UserManager,如下所示:

    var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    ApplicationUser u = userManager.FindById(User.Identity.GetUserId());
Run Code Online (Sandbox Code Playgroud)

我的项目符合(有点兴奋),但当我运行代码时,我收到以下错误:

Additional information: The entity type ApplicationUser is not part of the model for the current context.
Run Code Online (Sandbox Code Playgroud)

更新2:

我已将该功能移至IdentityModel(不要问我在这里抓住吸管),如下所示:

   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 Boolean IsRegComplete { get; set; }

        public void SetIsRegComplete(string userId, Boolean valueToSet)
        {

            var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
            ApplicationUser u = new ApplicationUser();
            u = userManager.FindById(userId);

            u.IsRegComplete = valueToSet;
            return;
        }
    }
Run Code Online (Sandbox Code Playgroud)

但是我仍然得到以下内容:

The entity type ApplicationUser is not part of the model for the current context.
Run Code Online (Sandbox Code Playgroud)

IdentitiesModels.cs中还有以下类:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
    public ApplicationDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}
Run Code Online (Sandbox Code Playgroud)

我在这做错了什么?感觉就像我正在咆哮着错误的树.我想要做的就是从不同控制器(即不是AccountsController)的操作更新aspnetuser表中的列.

Ira*_*chi 32

如果您使用的是默认项目模板,则会UserManager按以下方式创建获取:

在Startup.Auth.cs文件中,有一行如下:

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
Run Code Online (Sandbox Code Playgroud)

这使得OWIN管道实例化ApplicationUserManager每次请求到达服务器时的实例.您可以使用控制器内的以下代码从OWIN管道获取该实例:

Request.GetOwinContext().GetUserManager<ApplicationUserManager>()
Run Code Online (Sandbox Code Playgroud)

如果仔细查看您的AccountController课程,您将看到以下可访问ApplicationUserManager可能的代码:

    private ApplicationUserManager _userManager;

    public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }
Run Code Online (Sandbox Code Playgroud)

请注意,如果您需要实例化ApplicationUserManager该类,则需要使用ApplicationUserManager.Create静态方法,以便应用适当的设置和配置.

  • 我建议你从OWIN上下文中检索`UserManager`,而不是创建一个新实例,因为它在每个请求中都是创建的,而创建一个新实例是多余的开销. (2认同)

Muh*_*ram 5

如果必须在另一个Controller中获取UserManager的实例,只需将其参数添加到Controller的构造函数中,如下所示

public class MyController : Controller
{
    private readonly UserManager<ApplicationUser> _userManager;

    public MyController(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;;
    }
}
Run Code Online (Sandbox Code Playgroud)

但是我必须在不是控制器的类中获取UserManager!

任何帮助,将不胜感激。

更新

我正在考虑您正在使用asp.net核心