ASP.NET中主要项目的身份分离

时间:2016-07-02 02:36:15

标签: c# asp.net-mvc-5 entity-framework-6 asp.net-identity

所以我试图将Identity与我的主项目真正分开。从本质上讲,我的主要项目应该知道身份和身份不应该对我的主要项目有所了解。这允许我构建一组我可以在许多项目中使用的dll。我这样做的方式是使用Ninject注射。

所以我认为这是一个相当简单的设置,让我告诉你:

CustomIdentityDbContext:

public class CustomIdentityDbContext : IdentityDbContext<AzularisUser>, ICustomIdentityDbContext
{
    public CustomIdentityDbContext()
        : base("DefaultConnection", throwIfV1Schema: false)
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<IdentityUserClaim>().ToTable("UserClaim");
        modelBuilder.Entity<IdentityUserRole>().ToTable("UserRole");
        modelBuilder.Entity<IdentityUserLogin>().ToTable("UserLogin");
        modelBuilder.Entity<IdentityRole>().ToTable("Role");
        modelBuilder.Entity<AzularisUser>().ToTable("User");
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
    }

    public static DbContext Create()
    {
        return new CustomIdentityDbContext();
    }
}

AzularisUser:

public class AzularisUser : IdentityUser, IAzularisUser
{
    public string Alias { get; set; }
    public int EmailResendCount { get; set; }
    public DateTime EmailResendAttemptTime { get; set; }

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<AzularisUser> manager)
    {
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        //some code to add claims, removed as its not important to my demonstration
        return userIdentity;
    }
}

SignInService:

public class SignInService : SignInManager<AzularisUser, string>, ISignInService
{
    private readonly UserService _userService;

    public SignInService(UserService userService, IAuthenticationManager authenticationManager)
        : base(userService, authenticationManager)
    {
        _userService = userService;
    }
}

UserService:

public class UserService : UserManager<AzularisUser>, IUserService
{
    public UserService(IUserStore<AzularisUser> store, IDataProtectionProvider dataProvider)
        : base(store)
    {
        UserValidator = new UserValidator<AzularisUser>(this)
        {
            AllowOnlyAlphanumericUserNames = false,
            RequireUniqueEmail = true
        };

        PasswordValidator = new PasswordValidator
        {
            RequiredLength = 8,
            RequireNonLetterOrDigit = true,
            RequireDigit = true,
            RequireLowercase = true,
            RequireUppercase = true,
        };

        UserLockoutEnabledByDefault = true;
        DefaultAccountLockoutTimeSpan = TimeSpan.FromMinutes(1);
        MaxFailedAccessAttemptsBeforeLockout = 5;

        var dataProtectionProvider = dataProvider;
        if (dataProtectionProvider != null)
        {
            UserTokenProvider =
                new DataProtectorTokenProvider<AzularisUser>(dataProtectionProvider.Create("ASP.NET Identity"));
        }
    }
}

UserStore:

public class UserStore : UserStore<AzularisUser>
{
    public UserStore(IdentityDbContext context)
        : base(context)
    {
    }
}

IdentityManager:

public class IdentityManager : IIdentityManager
{
    public ISignInService SignInService { get; set; }
    public IUserService UserService { get; set; }
    public IAuthenticationManager AuthenticationService { get; set; }

    public IdentityManager(ISignInService signInService, IUserService userService, IAuthenticationManager authenticationService)
    {
        SignInService = signInService;
        UserService = userService;
        AuthenticationService = authenticationService;
    }
}

Startup.cs

public partial class Startup
{
    private IAppBuilder _app;
    public void Configuration(IAppBuilder app)
    {
        ConfigureAuth(app);
        _app = app;
        app.UseNinjectMiddleware(CreateKernel);
    }

    private IKernel CreateKernel()
    {
        var kernel = new StandardKernel();
        kernel.Load(Assembly.GetExecutingAssembly());
        kernel.Bind<ICustomIdentityDbContext>().To<ITournapediaDbContext>().InRequestScope();
        kernel.Bind<ITournapediaDbContext>().To<TournapediaDbContext>().InRequestScope();
        kernel.Bind<IUserStore<AzularisUser>>().To<UserStore>();
        kernel.Bind<IUserService>().To<UserService>();
        kernel.Bind<ISignInService>().To<SignInService>();
        kernel.Bind<IAuthenticationManager>().ToMethod(x => HttpContext.Current.GetOwinContext().Authentication);
        kernel.Bind<IDataProtectionProvider>().ToMethod(x => _app.GetDataProtectionProvider());
        kernel.Bind<IIdentityManager>().To<IdentityManager>();

        return kernel;
    }
}

我为长代码道歉,但有必要演示我的设置。我面临的问题是,如果我按原样运行此代码,并尝试执行诸如

之类的操作

await SignInService.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, true);

我会收到以下错误:

  

实体类型AzularisUser不是当前上下文模型的一部分。

如果我更新了我的UserStore并按如下方式设置了类:

public class UserStore : UserStore<AzularisUser>
{
    public UserStore(TournapediaDbContext context)
        : base(context)
    {
    }
}

代码有效,我没有问题。在UserStore延长IdentityDbContext之后,我不能完全理解为什么我不能告诉TournapediaDbContext IdentityDbContext使用TournapediaDbContext。我不能将其保留为Tournapedia,因为我的身份必须引用library(ggplot2) library(directlabels) df <- expand.grid(x=1:100, y=1:100) df$z <- df$x * df$y p <- ggplot(aes(x=x, y=y, z=z), data = df) + geom_raster(data=df, aes(fill=z), show.legend = TRUE) + scale_fill_gradient(limits=range(df$z), high = 'white', low = 'red') + geom_contour(aes(colour = ..level..)) + scale_colour_gradient(guide = 'none') p1 = direct.label(p, list("bottom.pieces", colour='black')) p1 项目,我正试图避免。

我将如何解决这个问题?

0 个答案:

没有答案