加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

asp.net-mvc – 在AccountController之外访问UserManager

发布时间:2020-12-15 23:35:21 所属栏目:asp.Net 来源:网络整理
导读:我试图从一个不同的控制器(不是accountcontroller)在aspnetuser表中设置一个列的值.我一直在尝试访问UserManager,但我无法想象我们如何做到这一点. 到目前为止,我已经在控制器中尝试了以下内容: ApplicationUser u = UserManager.FindById(User.Identity.Ge
我试图从一个不同的控制器(不是accountcontroller)在aspnetuser表中设置一个列的值.我一直在尝试访问UserManager,但我无法想象我们如何做到这一点.

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

ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
    u.IsRegComplete = true;
    UserManager.Update(u);

这不会编译(我认为因为UserManager没有被实例化的控制器)

我还试图在AccountController中创建一个公共方法来接受我想要更改值的值,然后在其中执行,但是我无法弄清楚如何调用它.

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

    return;
}

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

更新:

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

var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(db));
    ApplicationUser u = userManager.FindById(User.Identity.GetUserId());

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

Additional information: The entity type ApplicationUser is not part of the model for the current context.

更新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;
        }
    }

不过我还是得到以下几点:

The entity type ApplicationUser is not part of the model for the current context.

IdentitiesModels.cs中还有以下类:

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

    public static ApplicationDbContext Create()
    {
        return new ApplicationDbContext();
    }
}

我在这里做错了什么?感觉就像我完全吠着错误的树.所有我想要做的是从不同的控制器(即不是AccountsController)的操作更新aspnetuser表中的一列.

解决方法

如果您使用默认项目模板,则以下列方式创建UserManager:

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

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

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

Request.GetOwinContext().GetUserManager<ApplicationUserManager>()

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

private ApplicationUserManager _userManager;

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读