할 수 없을 얻을 경고:등급이 부서 OwinContext 에 apicontroller
-
20-12-2019 - |
문제
나는 다음과 같은 Microsoft 샘플을 구현하는 이메일 확인 Id2.0.0
나에 붙어 이 부분
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
이 작품에서 controller
지 HttpContext
가 포함되어 있지 않 GetOwinContext
메서드 ApiController.
그래서 나 HttpContext.Current.GetOwinContext()
그러나 이 방법 GetUserManager
이 존재하지 않습니다.
나는 알 수 없습을 얻을 수있는 방법 UserManager
내가에 구축 시작합니다.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
?
해결책
정말 오해 귀하의 질문 앞.당신은 단지 누락 일부를 사용하여 문,나는 생각한다.
이 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;
}
}
이 싱글 코드의 라인을 내 일...
var manager =
new ApplicationUserManager(new UserStore<ApplicationUser>(new ApplicationDbContext()));
당신은 그것을 사용할 수 있습에서 컨트롤러를 얻을 수 있는 조치를 인스턴스의합니다.
제휴하지 않습니다 StackOverflow