gpt4 book ai didi

c# - 使用 ASP.NET Identity 2.2.1 设置简单注入(inject)器

转载 作者:行者123 更新时间:2023-11-30 15:21:38 25 4
gpt4 key购买 nike

因此,我一直在尝试设置 ASP.NET Identity 2.2.1,但一直遇到障碍。对于应该由框架自动生成的东西来说,配置起来确实很复杂。我想我快到了,但我在使用 Simple Injector 设置 IoC 时仍然遇到问题。

我一直在阅读有关这些问题的其他 StackOverflow 问题和博客。它们中的大多数看起来已经过时了,因为它们处理的是 ASP.NET Identity 1.0。从 2.0 到 2.2.1 甚至有足够多的变化我遇到了一些过时解决方案的问题。或者解决方案涉及其他 IoC 框架,我无法将它们转换为 Simple Injector。我使用过其他 IoC 框架,但我是 Simple Injector 的新手。

更新 - Ric .Net's first link in his comment很有帮助。我已经重新格式化我的工作以适应他的建议,我想我快到了。但我还有一个问题。我已经用下面的代码替换了之前的代码。

1) 我更新了 ApplicationDbContext:

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

2) 我从 ApplicationUserManager 中删除了 Create() 方法,并将其移动到一个初始化我所有简单注入(inject)器代码的类中:

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)
{
var container = new Container();

container.RegisterSingle<IAppBuilder>(app);

container.RegisterPerWebRequest<
ApplicationUserManager>();

container.RegisterPerWebRequest<ApplicationDbContext>(()
=> new ApplicationDbContext(
"Your constring goes here"));

container.RegisterPerWebRequest<IUserStore<
ApplicationUser>>(() =>
new UserStore<ApplicationUser>(
container.GetInstance<ApplicationDbContext>()));

container.RegisterInitializer<ApplicationUserManager>(
manager => InitializeUserManager(manager, app));

container.RegisterMvcControllers(
Assembly.GetExecutingAssembly());

return container;
}

private static void InitializeUserManager(
ApplicationUserManager manager, IAppBuilder app)
{
manager.UserValidator =
new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};

//Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator()
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = true,
};

var dataProtectionProvider =
app.GetDataProtectionProvider();

if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<ApplicationUser>(
dataProtectionProvider.Create("ASP.NET Identity"));
}
}
}

3) 我更改了Startup.cs中的配置方法:

public void Configuration(IAppBuilder app)
{
var container = SimpleInjectorInitializer.Initialize(app);
ConfigureAuth(app, container);
}

4) 我更新了 Configure.Auth.cs 中的 ConfigureAuth:

public void ConfigureAuth(IAppBuilder app, Container container)
{
// Configure the db context and user manager to use a single instance per request
//app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext(container.GetInstance<ApplicationUserManager>);
// Rest of code here ...

5) 然后我从 AccountsController 中删除了默认构造函数和 UserManager 属性。

我现在遇到的问题是,自 ASP.NET Identity 2.0 以来,他们添加了参数 ISecureDataFormat<AuthenticationTicket> accessTokenFormat到 AccountsController 构造函数。

public AccountController(ApplicationUserManager userManager, 
ISecureDataFormat<AuthenticationTicket> accessTokenFormat)

那么现在,如何为 ISecureDataFormat<AuthenticationTicket> 添加 DI在简单注入(inject)器中?

最佳答案

于是求助Ric .Net (我在上面作为问题的一部分发布)和 Lucas Teles answer in another question我终于得到了答案。 Lucas 建议将这些行添加到我的 Simple Injector 设置中:

container.Register<ISecureDataFormat<AuthenticationTicket>,
SecureDataFormat<AuthenticationTicket>>(Lifestyle.Scoped);
container.Register<ITextEncoder, Base64UrlTextEncoder>(Lifestyle.Scoped);
container.Register<IDataSerializer<AuthenticationTicket>, TicketSerializer>(
Lifestyle.Scoped);
container.Register<IDataProtector>(
() => new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider()
.Create("ASP.NET Identity"),
Lifestyle.Scoped);

所以现在我的容器构建方法如下所示:

public static Container GetInitializeContainer(IAppBuilder app)
{
var container = new Container();

// IoC for ASP.NET Identity
container.RegisterSingleton<IAppBuilder>(app);
container.Register<ApplicationUserManager>(Lifestyle.Scoped);
container.Register<ApplicationDbContext>(
() => new ApplicationDbContext("Your constring goes here"),
Lifestyle.Scoped);
container.Register<IUserStore<ApplicationUser>>(
() => new UserStore<ApplicationUser>(
container.GetInstance<ApplicationDbContext>()),
Lifestyle.Scoped);
container.RegisterInitializer<ApplicationUserManager>(
manager => InitializeUserManager(manager, app));
// Setup for ISecureDataFormat
container.Register<ISecureDataFormat<AuthenticationTicket>,
SecureDataFormat<AuthenticationTicket>>(Lifestyle.Scoped);
container.Register<ITextEncoder, Base64UrlTextEncoder>(Lifestyle.Scoped);
container.Register<IDataSerializer<AuthenticationTicket>,
TicketSerializer>(Lifestyle.Scoped);
container.Register<IDataProtector>(
() => new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider()
.Create("ASP.NET Identity"),
Lifestyle.Scoped);

// Register all controllers
container.RegisterMvcControllers(Assembly.GetExecutingAssembly());

return container;
}

关于c# - 使用 ASP.NET Identity 2.2.1 设置简单注入(inject)器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37085776/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com