Moh*_*are 9 structuremap asp.net-mvc dependency-injection asp.net-identity asp.net-identity-2
我在我的项目中使用asp.net标识并使用structuremap作为DI框架.问题是当我使用构造函数注入时,ApplicationUserManager没有配置它的所有成员,例如TokenProvider,...
这是我的ApplicationUserManager类:
public class ApplicationUserManager : UserManager<User, long>
{
public ApplicationUserManager(IUserStore<User, long> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new CustomUserStore(context.Get<InsuranceManagementContext>()));
// Configure the application user manager
manager.UserValidator = new UserValidator<User, long>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = false
};
manager.PasswordValidator = new PasswordValidator
{
RequireDigit = true,
RequiredLength = 8,
RequireLowercase = false,
RequireNonLetterOrDigit = true,
RequireUppercase = false
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider =
new DataProtectorTokenProvider<User, long>(dataProtectionProvider.Create("TEST"));
}
return manager;
}
}
Run Code Online (Sandbox Code Playgroud)
这是Startup.Auth类:
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(InsuranceManagementContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
// Enable the application to use a cookie to store information for the signed in user
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
ExpireTimeSpan = TimeSpan.FromHours(2.0),
AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active,
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
});
}
}
Run Code Online (Sandbox Code Playgroud)
和我的AccountController:
public class AccountController : BaseController
{
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public AccountController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是我如何使用structuremap配置我的ApplicationUserManager?如果我把它设置为下面的代码它可以工作,但我不知道这是一个很好的解决方案:
ObjectFactory.Initialize(x =>
{
...
x.For<ApplicationUserManager>().Use(() => HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>());
...
});
Run Code Online (Sandbox Code Playgroud)
请提示我,如果有更好的解决方案,如果可以,那么它的最佳寿命是多少?HttpContextScope,Singleton,......?
在为此创建StructureMap配置之前,有助于了解如何手动创建它,即,如果您实际上自己"新建"了所有内容.
UserManager依赖于IUserStore,其EntityFramework实现(UserStore)依赖于DbContext.手动完成所有操作将如下所示:
var dbContext = new IdentityDbContext("Your ConnectionString Name");
var userStore = new UserStore<IdentityUser>(dbContext);
var userManager = new UserManager<IdentityUser>(userStore);
Run Code Online (Sandbox Code Playgroud)
(IdentityUser
如果您使用自定义用户,请替换为您的自定义用户)
然后你可以UserManager
像这样配置:
userManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6
};
Run Code Online (Sandbox Code Playgroud)
配置userManager最复杂的部分与UserTokenProvider(使用数据保护api)有关,如果你手动完成,它将如下所示:
var dataProtectionProvider = new DpapiDataProtectionProvider("Application name");
var dataProtector = dataProtectionProvider.Create("Purpose");
userManager.UserTokenProvider = new DataProtectorTokenProvider<IdentityUser>(dataProtector);
Run Code Online (Sandbox Code Playgroud)
这是一个StructureMap注册表的示例(您可以从此示例中进行推断并根据您自己的需要进行调整):
public DefaultRegistry() {
Scan(
scan => {
scan.TheCallingAssembly();
scan.WithDefaultConventions();
scan.With(new ControllerConvention());
});
For<IUserStore<IdentityUser>>()
.Use<UserStore<IdentityUser>>()
.Ctor<DbContext>()
.Is<IdentityDbContext>(cfg => cfg.SelectConstructor(() => new IdentityDbContext("connection string")).Ctor<string>().Is("IdentitySetupWithStructureMap"));
ForConcreteType<UserManager<IdentityUser>>()
.Configure
.SetProperty(userManager => userManager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6
})
.SetProperty(userManager => userManager.UserValidator = new UserValidator<IdentityUser>(userManager));
}
Run Code Online (Sandbox Code Playgroud)
我写了一篇关于此的博客文章,它解释了导致此配置的过程,还有一个链接到MVC项目的github上的示例,使用此配置,您可以创建,列出和删除用户.
归档时间: |
|
查看次数: |
16443 次 |
最近记录: |