Web API 2,Unity 2的Identity 2

时间:2017-03-27 08:18:16

标签: c# dependency-injection asp.net-web-api2 asp.net-identity unity-container

我正在使用带有身份2的web api 2创建API,我也使用Unity DI来解决我的依赖。

我的代码工作正常,但是当我尝试使用帐户控制器注册用户时,我的ApplicationUserManager验证无效。

这是我的代码

Unity Config.cs

public static class UnityConfig
{
    public static void RegisterComponents()
    {
        var container = new UnityContainer();

        RegisterTypes(container);
        // register all your components with the container here
        // it is NOT necessary to register your controllers

        // e.g. container.RegisterType<ITestService, TestService>();

        GlobalConfiguration.Configuration.DependencyResolver = 
            new UnityDependencyResolver(container);
    }

    private static void RegisterTypes(UnityContainer container)
    {
        container.RegisterType<DbContext, MyContext>(new HierarchicalLifetimeManager());
        container.RegisterType<UserManager<ApplicationUser, int>, ApplicationUserManager>();
        container.RegisterType<MyContext>(new HierarchicalLifetimeManager());
        container.RegisterType<ApplicationUserManager>();

        container.RegisterType<ITextEncoder, Base64UrlTextEncoder>();
        container.RegisterType<IDataSerializer<AuthenticationTicket>, TicketSerializer>();
        container.RegisterInstance(new DpapiDataProtectionProvider().Create("ASP.NET Identity"));
        container.RegisterType<ISecureDataFormat<AuthenticationTicket>, SecureDataFormat<AuthenticationTicket>>();

        container.RegisterType<IUserStore<ApplicationUser, int>, CustomUserStore>(
            new InjectionConstructor(typeof(MyContext)));
        container.RegisterType<IAuthenticationManager>(
            new InjectionFactory(o => HttpContext.Current.GetOwinContext().Authentication));
        container.RegisterType<IOwinContext>(new InjectionFactory(o => HttpContext.Current.GetOwinContext()));
    }
}

的Global.asax.cs

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();
    UnityConfig.RegisterComponents();
    GlobalConfiguration.Configure(WebApiConfig.Register);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    BundleConfig.RegisterBundles(BundleTable.Bundles);
}

我的ConfigureAuth函数Startup.Auth.cs

public void ConfigureAuth(IAppBuilder app)
{
    // Configure the db context and user manager to use a single instance per request
    app.CreatePerOwinContext(HostedScormContext.Create);
    app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

现在我的AccountController

private const string LocalLoginProvider = "Local";
private ApplicationUserManager _userManager;
public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }

public AccountController(ApplicationUserManager userManager,
    ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
    _userManager = userManager;
    AccessTokenFormat = accessTokenFormat;
}

[AllowAnonymous]
[Route("Register")]
public async Task<IHttpActionResult> Register(RegisterUserDto model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);
    }
    var user = new ApplicationUser()
    {
        FirstName = model.FirstName,
        LastName = model.LastName,
        UserName = model.Username,
        Email = model.Email,
        Address1 = model.Address1,
        Address2 = model.Address2,
        Country = model.Country,
        City = model.City,
        State = model.State,
        ZipCode = model.ZipCode,
        Description = model.Description
    };

    IdentityResult result = await _userManager.CreateAsync(user, model.Password);

    if (!result.Succeeded)
    {
        return GetErrorResult(result);
    }

    return Ok();
}

当我点击寄存器API时,它确实创建了一个用户,但没有验证用户就像电子邮件已经注册等等。虽然我的项目中有Identity config.cs但它似乎无效。

我还调试代码以检查UserManager对象,在那里我发现我的identityConfig设置以某种方式被替换为如果我设置唯一的电子邮件为true但在控制器中我得到唯一的电子邮件错误。

如何通过UNITY DI进行验证

0 个答案:

没有答案