finally find a solution the first solution: first: change the startup class bootstrap you must add singleInstance (); to avoid dependency errors in queries [No scope with matching tag "AutofacWebRequest]
builder.RegisterType<DatabaseContext>().AsSelf().SingleInstance(); builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => new IdentityFactoryOptions<ApplicationUserManager>() { DataProtectionProvider = new DpapiDataProtectionProvider("your app name") }); builder.RegisterType<ApplicationUserManager>().AsSelf().SingleInstance();
seconds: in Startup.cs will remove GlobalConfiguration.configuration.DependencyResolver because it always gives null. Therefore, I will use the autofac container autoloader, but should use it from lifetimecope, this container is returned from your autofac auto-loading method
OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), Provider = container.BeginLifetimeScope().Resolve<IOAuthAuthorizationServerProvider>(), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true };
thirdly: a constructor will be added to the ApplicationOAuthProvider class, which will take applicationUserManager as a parameter
this fix my zero error after two days google search and cannot find the answer, hope this helps.
second solution: since SingleInstance () is not suitable for enterprise applications, so you can use InstancePerRequest (); for all types of registers
builder.RegisterType<DatabaseContext>().AsSelf().InstancePerRequest(); builder.Register<IdentityFactoryOptions<ApplicationUserManager>>(c => new IdentityFactoryOptions<ApplicationUserManager>() { DataProtectionProvider = new DpapiDataProtectionProvider("your app name") }); builder.RegisterType<ApplicationUserManager>().AsSelf().InstancePerRequest();
in startup.cs
OAuthOptions = new OAuthAuthorizationServerOptions { TokenEndpointPath = new PathString("/Token"), // will instantiate new one to avoid Single Instance for resolving Provider = new CustomOAuthProvider(new ApplicationUserManager(new UserStore<Entities.ApplicationUser>(new DataContext()), AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(14), AllowInsecureHttp = true };
Class CustomOAuthProvider
using Microsoft.AspNet.Identity.Owin; using Microsoft.Owin.Security; using Microsoft.Owin.Security.OAuth; using System.Security.Claims; using System.Threading.Tasks; public class CustomOAuthProvider:OAuthAuthorizationServerProvider { private ApplicationUserManager _appUserManager; public CustomOAuthProvider(ApplicationUserManager appUserManager) { this._appUserManager = appUserManager; } public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) { var allowedOrigin = "*"; context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { allowedOrigin }); var userManager = new ApplicationUserManager(new Microsoft.AspNet.Identity.EntityFramework.UserStore<AppUser>(new Data.DataContext()),new IdentityFactoryOptions<ApplicationUserManager>(),new Data.Repositories.SettingRepository(new Data.Infrastructure.DbFactory())); AppUser user = await userManager.FindAsync(context.UserName, context.Password); if (user == null) { context.SetError("invalid_grant", "Invalid username or password."); return; } if (!user.IsActive) { context.SetError("invalid_activation", "Inactive account, contact support."); return; } if (!user.EmailConfirmed) { context.SetError("invalid_grant", "User did not confirm email."); return; } ClaimsIdentity oAuthIdentity = await userManager.GenerateUserIdentityAsync(user, "JWT"); AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, null); context.Validated(ticket); } public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context) { if (context.ClientId == null) { context.Validated(); } return Task.FromResult<object>(null); } }