使用NHibernate.AspNet.Identity [英] Using NHibernate.AspNet.Identity

查看:202
本文介绍了使用NHibernate.AspNet.Identity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用Asp.net的身份和NHibernate。

I am attempting to use Asp.net identity and NHibernate.

我已经使用.NET Framework 4.5.1创建一个新的空白Asp.net MVC的网站,我已经安装,并遵循此处所述使用的NuGet包NHibernate.AspNet.Identity的说明:

I have created a new blank Asp.net MVC site using .NET framework 4.5.1 and I have installed and followed the instructions for using nuget package NHibernate.AspNet.Identity as described here:

<一个href=\"https://github.com/milesibastos/NHibernate.AspNet.Identity\">https://github.com/milesibastos/NHibernate.AspNet.Identity

这涉及作出以下更改AccountController类默认构造函数:

which involves making the following changes to the AccountController class default constructor:

var mapper = new ModelMapper();
mapper.AddMapping<IdentityUserMap>();
mapper.AddMapping<IdentityRoleMap>();
mapper.AddMapping<IdentityUserClaimMap>();
mapper.AddMapping<IdentityUserLoginMap>();

var mapping = mapper.CompileMappingForAllExplicitlyAddedEntities();
var configuration = new Configuration();
configuration.Configure(System.Web.HttpContext.Current.Server.MapPath(@"~\Models\hibernate.cfg.xml"));
configuration.AddDeserializedMapping(mapping, null);

var schema = new SchemaExport(configuration);
schema.Create(true, true);

var factory = configuration.BuildSessionFactory();
var session = factory.OpenSession();

UserManager = new UserManager<ApplicationUser>(
                new UserStore<ApplicationUser>(session));

我收到以下异常:

I am getting the following exception:

没有持留为:IdentityTest.Models.ApplicationUser

No persister for: IdentityTest.Models.ApplicationUser

该ApplicationUser类没有任何附加属性IdentityUser(这对于一个实体框架的实现Asp.net身份正常工作)。

The ApplicationUser class doesn't have any additional properties to IdentityUser (which works fine for a Entity Framework implementation of Asp.net Identity).

任何人都可以提出建议,我怎么能得到Asp.net的身份与这个包的NuGet工作?

Can anyone offer suggestions as to how I can get Asp.net identity to work with this NuGet package?

推荐答案

我一直在努力非常符合这个库,这是使我怀疑这是为什么使用OWIN与NHibernate推荐的库。

I have struggled very much with this library, which is making me question why this is the recommended library for using OWIN with NHibernate.

总之,要回答你的问题,你只要你从GitHub的网站得到了code增加了对库中的类NHibernate的映射。 NHibernate的不具有 ApplicationUser 的映射,它只有一个映射为它的基类。 NHibernate的需要为实例化类的映射。这是有问题的,因为你没有访问库的组件映射code,所以你不能改变它使用 ApplicationUser 类代替。所以唯一的办法,让过去这个使用库,因为它是被删除的 ApplicationUser 类,并使用图书馆的 IdentityUser 类。或者,你可以从GitHub复制的映射code,并尝试使用 ApplicationUser 相同的映射。

Anyway, to answer your question, the code you provided that you got from the github website adds NHibernate mappings for the library's classes. NHibernate doesn't have a mapping for ApplicationUser, it only has a mapping for it's base class. NHibernate needs a mapping for the instantiated class. This is problematic because you don't have access to the mapping code in the library's assembly, so you can't change it to use the ApplicationUser class instead. So the only way to get past this using the library as it is, is to remove the ApplicationUser class and use the library's IdentityUser class. Or, you could copy the mapping code from github and try using the same mapping for ApplicationUser.

此外,库code和code,他给出了的AccountController 不会永远打开NHibernate的交易,所以尽管库调用 Session.Save Session.Update 数据不会最终将保存在数据库中。当您打开会话,你需要打开一个事务并将其保存为在类的私人领域:

Also, the library code and the code he gives for the AccountController does not ever open an NHibernate transaction, so even though the library calls Session.Save and Session.Update the data won't ultimately be saved in the database. After you open the session you need to open a transaction and save it as a private field on the class:

transaction = session.BeginTransaction(IsolationLevel.ReadCommitted);

然后,你需要调用器transaction.commit()之后您在的AccountController 执行完毕,所以你动作将需要重写 OnResultExecuted

Then you need to call transaction.Commit() after your action in the AccountController finishes executing, so you will need to override OnResultExecuted:

protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
    transaction.Commit();
}

请这个例子是过于简单化,并在生产中的应用,你需要有错误检查,你会回滚,而不是提交如果有错误,你需要正确的关闭/处理一切等。

Keep in mind this example is oversimplified, and in a production application you need to have error checking where you will Rollback instead of Commit if there are errors, and you need to properly close/dispose of everything, etc.

此外,还解决这些问题,即使后,存在与库中的其他问题。我最后不得不从GitHub下载源代码,所以我可以为了使用它修改库。有在图书馆的code。至少3其他明显的错误:

Furthermore, even after you solve those problems, there are other issues with the library. I ended up having to download the source from github so I could modify the library in order to use it. There are at least 3 other blatant errors in the library's code:

1)的 NHibernate.AspNet.Identity.UserStore

public virtual async Task<TUser> FindAsync(UserLoginInfo login)
{
    this.ThrowIfDisposed();
    if (login == null)
        throw new ArgumentNullException("login");

    IdentityUser entity = await Task.FromResult(Queryable
        .FirstOrDefault<IdentityUser>(
            (IQueryable<IdentityUser>)Queryable.Select<IdentityUserLogin, IdentityUser>(
                Queryable.Where<IdentityUserLogin>(
                    // This line attempts to query nhibernate for the built in asp.net
                    // UserLoginInfo class and then cast it to the NHibernate version IdentityUserLogin, 
                    // which always causes a runtime error. UserLoginInfo needs to be replaced 
                    // with IdentityUserLogin
                    (IQueryable<IdentityUserLogin>)this.Context.Query<UserLoginInfo>(), (Expression<Func<IdentityUserLogin, bool>>)(l => l.LoginProvider == login.LoginProvider && l.ProviderKey == login.ProviderKey)),
                    (Expression<Func<IdentityUserLogin, IdentityUser>>)(l => l.User))));

    return entity as TUser;
}

2)的 NHibernate.AspNet.Identity.DomainModel.ValueObject

protected override IEnumerable<PropertyInfo> GetTypeSpecificSignatureProperties()
{
    var invalidlyDecoratedProperties =
        this.GetType().GetProperties().Where(
            p => Attribute.IsDefined(p, typeof(DomainSignatureAttribute), true));

    string message = "Properties were found within " + this.GetType() +
                                        @" having the
            [DomainSignature] attribute. The domain signature of a value object includes all
            of the properties of the object by convention; consequently, adding [DomainSignature]
            to the properties of a value object's properties is misleading and should be removed. 
            Alternatively, you can inherit from Entity if that fits your needs better.";

    // This line is saying, 'If there are no invalidly decorated properties, 
    // throw an exception'..... which obviously should be the opposite, 
    // remove the negation (!)
    if (!invalidlyDecoratedProperties.Any())
        throw new InvalidOperationException(message);

    return this.GetType().GetProperties();
}

3) NHibernate.AspNet.Identity.UserStore :由于某些原因,使用一个外部供应商如Facebook,创建一个用户/用户登录至少在当最初创建的用户/用户登录,更新方法被调用,而不是添加/创建导致NHibernate的尝试更新不存在的实体。现在,不看多了进去,在 UserStore 更新方法,我改变了图书馆的code调用 SaveOrUpdate 在NHibernate会话,而不是更新这解决了这一问题。

3) In NHibernate.AspNet.Identity.UserStore: For some reason, at least when creating a user/user login using an external provider like facebook, when the user/user login is initially created, the Update method is called instead of the Add/Create causing NHibernate to try to update an entity that doesn't exist. For now, without looking more into it, in the UserStore update methods I changed the library's code to call SaveOrUpdate on the NHibernate session instead of Update which fixed the problem.

我只跑了,我的变化后,已经工作的图书馆简单的测试,所以没有告诉其他许多运行/逻辑错误是如何在这个库。发现这些错误后,这让我真的很紧张,现在使用它。看来,绝对没有与测试即使是简单的情况下完成的。使用此库一定要小心。

I have only ran simple tests with the library that have worked after my changes, so there is no telling how many other runtime / logic errors are in this library. After finding those errors, it makes me really nervous using it now. It seems there was absolutely no testing done with even simple scenarios. Take caution using this library.

这篇关于使用NHibernate.AspNet.Identity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆