asp.net-mvc – 在AccountController之外访问UserManager
|
我试图从一个不同的控制器(不是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静态方法,以便对其应用适当的设置和配置. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – AsyncPostBackTrigger和PostBackTrigger有什么区
- asp.net-mvc – MVC2 Binding不适用于Html.DropDownListFor
- asp.net-mvc-4 – 在ASP.NET MVC中包文件位置
- .net – 为什么我的剃刀视图抱怨“}预计”升级到剃刀2/mvc
- asp.net – 升级到.net 4.0后无法加载文件或程序集“AjaxCo
- asp.net-mvc – asp.net mvc中的动态子域
- asp.net – 增加IIS7 / Win7 Pro上的最大上传文件大小
- asp.net-mvc-3 – 如何在Mvc3中填充ListBoxFor?
- asp.net核心 – ASP.Net MVC 6中使用Tag Helpers的优点是什
- [asp.net mvc 奇淫巧技] 02 - 巧用Razor引擎在Action内生成
- asp.net中的SqlDependency
- asp.net-mvc – System.Web.Mvc.ActionFilterAtt
- asp.net-mvc – 运行Asp.Net Mvc作为OWIN中间件?
- 将asp.net应用程序移植到天蓝色有多大挑战?
- asp.net – 我们可以在网页中使用多种形式吗?
- asp.net – 可以通过移动设备的Web浏览器上传图片
- asp.net-mvc – 使旧会话Cookie无效 – ASP.Net标
- 将“onclick”属性添加到asp.net下拉列表项
- asp.net-mvc-3 – MVC3,多文件上传,模型绑定
- asp.net – 如何知道一个javascript文件是否已被
