APIコントローラーのOwinContextからUserManagerを取得できません
-
20-12-2019 - |
質問
Microsoft のサンプルに従って、Identity 2.0.0 で電子メール検証を実装しています。
この部分で行き詰まっています
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
これは、 controller
しかし HttpContext
何も含まれていません GetOwinContext
のメソッド APIコントローラー.
それで私は試してみました HttpContext.Current.GetOwinContext()
しかし、その方法 GetUserManager
存在しません。
を入手する方法がわかりません UserManager
内蔵します Startup.Auth.cs
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
//Configure the db context, user manager and role manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
...
}
この行
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
次の関数を呼び出して設定します。 UserManager
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<ApplicationDbContext>()));
//Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure user lockout defaults
manager.UserLockoutEnabledByDefault = true;
manager.DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(5);
manager.MaxFailedAccessAttemptsBeforeLockout = 5;
manager.EmailService = new EmailService();
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
どうすればこれにアクセスできますか UserManager
で ApiController
?
解決
先ほどの質問を本当に誤解していました。using ステートメントがいくつか欠けているだけだと思います。
の GetOwinContext().GetUserManager<ApplicationUserManager>()
いる Microsoft.AspNet.Identity.Owin
.
そこで、この部分を追加してみてください。
using Microsoft.AspNet.Identity.Owin;
using Microsoft.AspNet.Identity; // Maybe this one too
var manager = HttpContext.Current.GetOwinContext().GetUserManager<UserManager<User>>();
他のヒント
コントローラーの単体テストを行う場合は、この拡張メソッドの方が良い解決策になる可能性があります。
using System;
using System.Net.Http;
using System.Web;
using Microsoft.Owin;
public static IOwinContext GetOwinContext(this HttpRequestMessage request)
{
var context = request.Properties["MS_HttpContext"] as HttpContextWrapper;
if (context != null)
{
return HttpContextBaseExtensions.GetOwinContext(context.Request);
}
return null;
}
使用法:
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
この 1 行のコードが私の命を救ってくれました...
var manager =
new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
コントローラー アクション内でこれを使用して、UserManager のインスタンスを取得できます。
所属していません StackOverflow