c# - ApplicationUserManager.Create 在每个请求上调用

标签 c# asp.net asp.net-mvc asp.net-identity-2

我正在使用 asp.net mvc 5 与外部供应商 owin 提供(facebook,twitter)

ApplicationUserManager.Create 在每次请求时被调用。那里有很多登录用户不必要的东西(密码验证器配置或短信和电子邮件服务配置....)

var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
            // Configure validation logic for usernames
            manager.UserValidator = new UserValidator<ApplicationUser>(manager)
            {
                AllowOnlyAlphanumericUserNames = true,
                RequireUniqueEmail = true
            };

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

另外,我觉得跟这个有关系

public partial class Startup
    {    
        public void ConfigureAuth(IAppBuilder app)
      // Configure the db context, user manager and signin manager to use a  single instance per request
        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
...

我该怎么做才能让这个“创建”只在需要时被调用。我不想在不需要时实例化密码验证器和其他东西

谢谢

最佳答案

不确定这是否仍然打开,但是...我也不喜欢每次调用 ApplicationUserManager 的方式,所以我决定将其更改为单例。

对象的dispose方法会被自动调用,除非你在传入create的时候也传入一个析构函数回调。

所以我的 ApplicationUserManager 现在看起来像这样:

public class ApplicationUserManager : UserManager<SmartUser, Guid>
{

    //This manager seems to get re-created on every call! So rather keep a singleton...
    private static ApplicationUserManager _userManager;

    private ApplicationUserManager(IUserStore<SmartUser, Guid> store)
        : base(store)
    {
    }

    internal static void Destroy(IdentityFactoryOptions<ApplicationUserManager> options, ApplicationUserManager manager)
    {
        //We don't ever want to destroy our singleton - so just ignore
    }

    internal static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    {
        if (_userManager == null)
        {
            lock (typeof(ApplicationUserManager))
            {
                if (_userManager == null)
                    _userManager = CreateManager(options, context);
            }
        }

        return _userManager;
    }

    private static ApplicationUserManager CreateManager(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
    { 
       ... your existing Create code ...
    }
}

在 Startup.Auth 中,我传入了 Destroy 回调

        app.CreatePerOwinContext(ApplicationDbContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create, ApplicationUserManager.Destroy);
        app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);

另一种选择是忽略“每个请求调用的注册代码”并使 UserStore 成为单例....

尚未发现这种强硬方法的缺点...

关于c# - ApplicationUserManager.Create 在每个请求上调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33764891/

相关文章:

c# - 如何捕获文本框的值并将其用作变量?

javascript - 刷新页面后SignalR收到越来越多的消息

c# - 如何在 ASP.NET MVC 中有效地提供图像

jquery - HTML5 与 jQuery 验证(对于输入...)

c# - MVC - Linq - 使用另一个表中的记录填充 List<T>

c# - Braintree所有交易搜索

C# 与 C++ 安全性

javascript - Bundler 和 Minifier 未在 Visual Studios 2017 中缩小 JavaScript 文件

asp.net-mvc - MVC Identity (2.0.1) 中的 regenerateIdentity/validateInterval 持续时间后忽略 ExpireTimeSpan

c# - Devexpress C# XtraGrid 单细胞编辑问题