大家好!
在阅读了我上一个问题的MRebati提示后,我搜索并理解了不将unity和OWIN混合的重要性,以及如何依赖注入ASP.net身份。
首先,我不想被标记为问同样的问题,即使我是,但是现在我的代码与上一个问题相比有了一点变化。
现在我得到了一个不同的错误信息:
对象引用未设置为对象的实例。
这意味着当调用我的自定义Usermanager时,我仍然得到null。
我期望的结果是获得所有用户及其接收角色的列表:
XXXXXX ------ Admin
YYYYYY ------ Manager
关于我的项目的信息:
MVC: 5.2.3
身份:2.2.1
实体框架:代码优先2.2.1
我也试图使用Unity依赖注入。
下面是我的代码:
控制器:
public ActionResult GetUsersWithRoles(string roleName)
{
ViewBag.UsersWithRoles = _userManager.GetUsersInRole(context, roleName);
return View(ViewBag.UsersWithRoles);
}
我的自定义用户管理器在IdentityConfig.cs:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
/// the create part which is moved from the default ///
public IQueryable<ApplicationUser> GetUsersInRole(ApplicationDbContext context, string roleName)
{
if (context !=null && roleName !=null)
{
var roles = context.Roles.Where(r => r.Name == roleName);
if (roles.Any())
{
var roleId = roles.First().Id;
return from user in context.Users
where user.Roles.Any(r => r.RoleId == roleId)
select user;
}
}
return null;
}
最后我的观点:
@using (Html.BeginForm("GetUsersWithRoles", "Roles"))
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<h3>Roles for this user </h3>
foreach (IQueryables in ViewBag.UsersWithRoles)
{
<tr>
<td>@s</td>
</tr>
}
}
我的unityConfig.cs
using System;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;
using eCommerce.DAL.Repositories;
using eCommerce.Model;
using eCommerce.Contracts.Repositories;
using eCommerce.WebUI.Controllers;
using eCommerce.WebUI.Models;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
namespace eCommerce.WebUI.App_Start
{
public class UnityConfig
{
#region Unity Container
private static Lazy<IUnityContainer> container = new Lazy<IUnityContainer>(() =>
{
var container = new UnityContainer();
RegisterTypes(container);
return container;
});
public static IUnityContainer GetConfiguredContainer()
{
return container.Value;
}
#endregion
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<IRepositoryBase<Customer>, CustomerRepository>();
container.RegisterType<IRepositoryBase<Product>, ProductRepository>();
container.RegisterType<IRepositoryBase<Basket>, BasketRepository>();
container.RegisterType<IRepositoryBase<Voucher>, VoucherRepository>();
container.RegisterType<IRepositoryBase<VoucherType>, VoucherTypeRepository>();
container.RegisterType<IRepositoryBase<BasketVoucher>, BasketVoucherRepository>();
container.RegisterType<IRepositoryBase<BasketItem>, BasketItemsRepository>();
//container.RegisterType<AccountController>(new InjectionConstructor());
container.RegisterType<ApplicationDbContext>(new PerRequestLifetimeManager());
container.RegisterType<ApplicationUserManager>();
container.RegisterType <IUserStore<ApplicationUser>, UserStore<ApplicationUser>>(new InjectionConstructor(typeof(ApplicationDbContext)));
}
}
}
下面是RolesController.cs的构造函数:
private ApplicationDbContext context;
private ApplicationUserManager _userManager;
public RolesController(ApplicationDbContext context, ApplicationUserManager _userManager)
{
this.context = context;
this._userManager = _userManager;
}
以下是我的startup.cs:ssembly: OwinStartupAttribute(typeof(eCommerce.WebUI.Startup))]
namespace eCommerce.WebUI
{
public partial class Startup
{
internal static IDataProtectionProvider DataProtectionProvider { get; private set; }
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
//app.CreatePerOwinContext(ApplicationDbContext.Create);
//app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext(() => DependencyResolver.Current.GetService<ApplicationUserManager>()); // <-
DataProtectionProvider = app.GetDataProtectionProvider();
}
}
}
这是我的全局。asax:
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AreaRegistration.RegisterAllAreas();
var context = new ApplicationDbContext();
if (!context.Users.Any(user => user.UserName == "Email@hotmail.com"))
{
var userStore = new UserStore<ApplicationUser>(context);
var userManager = new UserManager<ApplicationUser>(userStore);
var applicationUser = new ApplicationUser() { UserName = "Email@hotmail.com" };
userManager.Create(applicationUser, "Password");
var roleStore = new RoleStore<IdentityRole>(context);
var roleManager = new RoleManager<IdentityRole>(roleStore);
roleManager.Create(new IdentityRole("Admin"));
userManager.AddToRole(applicationUser.Id, "Admin");
}
}
}
从我的rolesController创建方法:
[HttpPost]
public ActionResult Create(FormCollection collection)
{
try
{
context.Roles.Add(new IdentityRole
{
Name = collection["RoleName"]
});
context.SaveChanges();
ViewBag.ResultMessage = "Role created successfully !";
return RedirectToAction("Index");
}
catch
{
return View();
}
}
谢谢你的时间。
最好在UserManager
中保留对DbContext的引用:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
private readonly ApplicationDbContext context;
public ApplicationUserManager(ApplicationDbContext context, /*... other parameters*/) : base(store)
{
this.context = context;
//... other configuration bits
}
public IEnumerable<ApplicationUser> GetUsersInRole(string roleName)
{
if (String.IsNullOrWhiteSpace(roleName))
{
throw new ArgumentNullException(nameof(roleName));
}
var role = context.Roles.FirstOrDefault(r => r.Name == roleName);
if (role == null)
{
throw new Exception($"Role with this name not found: {roleName}");
}
var users = context.Users.Where(u => u.Roles.Any(r => r.RoleId == role.Id)).ToList();
return users;
}
// the rest of User manager methods
}
在您的控制器中避免使用ViewBag
-这不是一个好的做法。此外,正如已经提到的,永远不要将IQueryable
传递到您的视图。粗略地说,IQueryable
是一个SQL查询,但IEnumerable是一个对象集合。视图只需要知道对象。所以你的控制器可以像这样:
public class UsersWithRoleController : Controller
{
private readonly ApplicationUserManager userManager;
public UsersWithRoleController(ApplicationUserManager userManager)
{
this.userManager = userManager;
}
public ActionResult GetUsersWithRoles(string roleName)
{
var users = userManager.GetUsersInRole(roleName);
var viewModel = new GetUsersWithRolesViewModel()
{
RoleName = roleName,
Users = users,
};
return View(viewModel);
}
}
public class GetUsersWithRolesViewModel
{
public String RoleName { get; set; }
public IEnumerable<ApplicationUser> Users { get; set; }
}
和视图将是:
@model IoCIdentity.Controllers.GetUsersWithRolesViewModel
@{
ViewBag.Title = "title";
}
<h2>List of users in role @Model.RoleName</h2>
<ul>
@foreach (var user in @Model.Users)
{
<li>@user.UserName</li>
}
</ul>
你可以在我的github上得到完整的样本。虽然我还没有测试它正确-我的db是坏了,在那一刻-(