認証に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()
メソッドは、IdentityFactoryOptions
とIOwinContext
をパラメータとして使用します。
私はこの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
の実装を変更
- これは、
ApplicationUserManager
を IdentiServerのDIフレームワークに登録する正しい方法ですか? - 登録時に
DataProtectionProvider
、コンストラクタ内にUserTokenProvider
を作成しています。この2つのプロバイダがIdentityServerによってどのように使用されていますか? - ApplicationUserManagerのコンストラクタではそれ以上必要としないので、
IOwinContext
のどこにも登録していないことに注意してください。 は、OWINパイプラインの問題を引き起こしますか? ApplicationUserManager
の理想的な登録モードは何ですか?
これは役立ちます:http://tech.trailmax。info/2014/09/aspnet-identity-and-ioc-container-registration/ – trailmax
@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