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

前端之家收集整理的这篇文章主要介绍了asp.net-mvc – 在AccountController之外访问UserManager前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图从一个不同的控制器(不是accountcontroller)在aspnetuser表中设置一个列的值.我一直在尝试访问UserManager,但我无法想象我们如何做到这一点.

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

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

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

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

  1. public void setIsRegComplete(Boolean setValue)
  2. {
  3. ApplicationUser u = UserManager.FindById(User.Identity.GetUserId());
  4. u.IsRegComplete = setValue;
  5. UserManager.Update(u);
  6.  
  7. return;
  8. }

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

更新:

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

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

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

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

更新2:

我已经将功能移到了IdentityModel(不要问我在这里抓着吸管),就像这样:

  1. public class ApplicationUser : IdentityUser
  2. {
  3. public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
  4. {
  5. // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
  6. var userIdentity = await manager.CreateIdentityAsync(this,DefaultAuthenticationTypes.ApplicationCookie);
  7. // Add custom user claims here
  8. return userIdentity;
  9. }
  10. public Boolean IsRegComplete { get; set; }
  11.  
  12. public void SetIsRegComplete(string userId,Boolean valueToSet)
  13. {
  14.  
  15. var userManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>());
  16. ApplicationUser u = new ApplicationUser();
  17. u = userManager.FindById(userId);
  18.  
  19. u.IsRegComplete = valueToSet;
  20. return;
  21. }
  22. }

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

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

IdentitiesModels.cs中还有以下类:

  1. public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  2. {
  3. public ApplicationDbContext()
  4. : base("DefaultConnection",throwIfV1Schema: false)
  5. {
  6. }
  7.  
  8. public static ApplicationDbContext Create()
  9. {
  10. return new ApplicationDbContext();
  11. }
  12. }

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

解决方法

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

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

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

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

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

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

  1. private ApplicationUserManager _userManager;
  2.  
  3. public ApplicationUserManager UserManager
  4. {
  5. get
  6. {
  7. return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
  8. }
  9. private set
  10. {
  11. _userManager = value;
  12. }
  13. }

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

猜你在找的asp.Net相关文章