因此,我一直在尝试设置 ASP.NET Identity 2.2.1,但一直遇到障碍。作为应该由框架自动生成的东西,配置起来确实很复杂。我想我已经快完成了,但在使用简单注入器设置 IoC 时仍然遇到问题。
我一直在阅读有关这些问题的其他 StackOverflow 问题和博客。其中大多数看起来已经过时了,因为它们处理的是 ASP.NET Identity 1.0。从 2.0 到 2.2.1 甚至有足够的变化,我遇到了一些过时解决方案的问题。或者这些解决方案涉及其他 IoC 框架,但我无法将它们转换为简单注入器。我使用过其他 IoC 框架,但我是简单注入器的新手。
更新- Ric .Net 在他的评论中的第一个链接非常有帮助。我已经重新格式化了我的工作以适应他的建议,我想我已经快完成了。但我仍然有一个问题。我已完成用下面的代码替换以前的代码。
1)我更新了ApplicationDbContext:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext(string connectionstring)
: base(connectionstring, throwIfV1Schema: false)
{
}
}
Run Code Online (Sandbox Code Playgroud)
2)我从ApplicationUserManager中删除了 Create() 方法,并将其移至一个初始化所有简单注入器代码的类中:
public static class SimpleInjectorInitializer
{
public static Container Initialize(IAppBuilder app)
{
var container = GetInitializeContainer(app);
container.Verify();
DependencyResolver.SetResolver(
new SimpleInjectorDependencyResolver(container));
return container;
}
public static Container GetInitializeContainer(
IAppBuilder app)
{ …Run Code Online (Sandbox Code Playgroud) 我正在关注Onion Architecture并使用Identity Framework.在我的核心项目中,我有:
public interface IUserRepository : IDisposable
{
// Repository methods.......
}
Run Code Online (Sandbox Code Playgroud)
在我的Architecture.Repository中,我有
public class UserRepository : IUserRepository
{
// This is Identity UserManager
private readonly UserManager<AppUser, int> _userManager;
private readonly IAuthenticationManager _authenticationManager;
private bool _disposed;
public UserRepository(UserManager<User, int> userManager,
IAuthenticationManager authenticationManager)
{
_userManager = userManager;
_authenticationManager = authenticationManager;
}
}
Run Code Online (Sandbox Code Playgroud)
在我的依赖解析项目中,我有:
[assembly: WebActivatorEx.PreApplicationStartMethod(typeof(IocConfig),
"RegisterDependencies")]
namespace AdShad.Infrastructure.DependencyResolution
{
public class IocConfig
{
public static void RegisterDependencies()
{
var container = new Container();
container.RegisterWebApiRequest<IUnitOfWork, UnitOfWork>();
container.RegisterWebApiRequest<IUserRepository, UserRepository>();
container.RegisterManyForOpenGeneric(typeof(IRepository<>),
typeof(BaseRepository<>).Assembly);
container.RegisterWebApiRequest<IEntitiesContext, …Run Code Online (Sandbox Code Playgroud) c# asp.net dependency-injection simple-injector asp.net-identity
好的,我之前的问题/设置有太多的变量,所以我将其分解为简单的骨骼组件。
鉴于以下使用 StructureMap3 的代码...
//IoC setup
For<HttpContextBase>().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null ));
For<ICurrentUser>().Use<CurrentUser>();
//Classes used
public class CurrentUser : ICurrentUser
{
public CurrentUser(HttpContextBase httpContext)
{
if (httpContext == null) return;
if (httpContext.User == null) return;
var user = httpContext.User;
if (!user.Identity.IsAuthenticated) return;
UserId = httpContext.User.GetIdentityId().GetValueOrDefault();
UserName = httpContext.User.Identity.Name;
}
public Guid UserId { get; set; }
public string UserName { get; set; }
}
public static class ClaimsExtensionMethods
public static Guid? GetIdentityId(this IPrincipal …Run Code Online (Sandbox Code Playgroud) c# structuremap asp.net-mvc dependency-injection structuremap3