2017-03-02 2 views
2

認証にIdentityServer3を使用しています。私はMicrosoft.AspNet.Identityフレームワークを実際の認証に使用しています。同じ目的のために、自分でApplicationUserManagerクラスを作成しました。ApplicationServerManagerをIdentityServer DIフレームワークに登録するにはどうすればいいですか?

AspNet IDには、OWINミドルウェアに統合されたIoC機能があります。それはApplicationUserManager

public static ApplicationUserManager 
Create(IdentityFactoryOptions<ApplicationUserManager> options, 
     IOwinContext context) 
    { 
     var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>())); 

の新しいインスタンスを返す関数デリゲートは、しかし、IdentityServerは、我々はできません(私はを考える)独自のDIフレームワークを使用し、かかる

app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create); 

:ように、それはApplicationUserManagerを登録します静的Create()メソッドを使用してApplicationUserManagerをIdentityServerに登録し、またCreate()メソッドは、IdentityFactoryOptionsIOwinContextをパラメータとして使用します。

私はこのSO postに続き、私はコンストラクタ・インジェクションに

public class ApplicationUserManager : UserManager<ApplicationUser, string> 
{ 
    public ApplicationUserManager(ApplicationUserStore store, IdentityFactoryOptions<ApplicationUserManager> options) 
     : base(store) 
    {   
     // Configure validation logic for usernames 
     UserValidator = new UserValidator<ApplicationUser>(this) 
     { 
      AllowOnlyAlphanumericUserNames = false, 
      RequireUniqueEmail = true 
     }; 

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

     // Configure user lockout defaults 
     UserLockoutEnabledByDefault = true; 
     DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5); 
     MaxFailedAccessAttemptsBeforeLockout = 5; 

     EmailService = new EmailService(); 
     SmsService = new SmsService(); 

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

を使用し、

factory.UserService = new Registration<IUserService, UserService>(); 
factory.Register(new Registration<ApplicationUserManager>()); 
factory.Register(new Registration<ApplicationUserStore>()); 
factory.Register(new Registration<IdentityFactoryOptions<ApplicationUserManager>>(resolver => new IdentityFactoryOptions<ApplicationUserManager> 
      { 
       DataProtectionProvider = new Microsoft.Owin.Security.DataProtection.DpapiDataProtectionProvider("ASP.NET Identity") 
      })); 
factory.Register(new Registration<ApplicationDbContext>(resolver => new ApplicationDbContext(ApplicationConfig.ConnectionString))); 

質問

以下のようIdentityServer自身のDIフレームワークですべてのサービスを登録するにはApplicationUserManagerの実装を変更
  1. これは、ApplicationUserManagerを IdentiServerのDIフレームワークに登録する正しい方法ですか?
  2. 登録時にDataProtectionProvider、コンストラクタ内にUserTokenProviderを作成しています。この2つのプロバイダがIdentityServerによってどのように使用されていますか?
  3. ApplicationUserManagerのコンストラクタではそれ以上必要としないので、IOwinContextのどこにも登録していないことに注意してください。 は、OWINパイプラインの問題を引き起こしますか?
  4. ApplicationUserManagerの理想的な登録モードは何ですか?
+1

これは役立ちます:http://tech.trailmax。info/2014/09/aspnet-identity-and-ioc-container-registration/ – trailmax

+0

@trailmax私の質問を投稿した後、あなたの記事を読むhttp://tech.trailmax.info/2014/06/asp-net-identity-実行中のあなたのsite-on-microsoft-azure-web-sites /から暗号解読を取得していたためです。そこで私はIDataProtectionProviderをStartup.csの 'internal static 'として提案に従って登録します。しかし、私の 'ApplicationUserManager'は別のアセンブリにありますので、' IDataProtectionProvider'を 'factory.Register(新規登録(リゾルバ=> Startup.DataProtectionProvider)) 'として登録しなければなりませんでした;' – LP13

答えて

2

これら二つの記事は、私のApplicationUserManagerは別々のクラスライブラリであるとstartup.csは、Webプロジェクトであるしかし私の問題

http://tech.trailmax.info/2014/06/asp-net-identity-and-cryptographicexception-when-running-your-site-on-microsoft-azure-web-sites/

http://tech.trailmax.info/2014/09/aspnet-identity-and-ioc-container-registration/

を解決するために助けました。クラスライブラリには、Webプロジェクトへの参照がありません。だから私は

public ApplicationUserManager(ApplicationUserStore store, IDataProtectionProvider dataProtectionProvider) 
     : base(store) 
    { 
     // other stuff 


     if (dataProtectionProvider != null) 
     { 
      UserTokenProvider = 
       new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("UserToken")); 
     } 
    } 

コンストラクタ

ApplicationUserManagerをリファクタリングしてもDIフレームワークに IDataProtectionProviderを登録しました。私はUnityをIoCとして使用していません。私はIdentityServer独自のDIフレームワークを使用しています。だから私は IDataProtectionProviderとして登録

factory.Register(new Registration<IDataProtectionProvider>(resolver => Startup.DataProtectionProvider)); 
+0

ああ、毎回!あなたはそれをソートしておいてよかった! – trailmax

関連する問題