gpt4 book ai didi

c# - 将 NHibernate 与 SQL Server 应用程序角色一起使用

转载 作者:太空宇宙 更新时间:2023-11-03 11:28:46 25 4
gpt4 key购买 nike

我正在使用 NHibernate (Fluently) 与一个数据库进行交互,该数据库被锁定为除应用程序角色外的所有角色。

我可以使用存储过程在 SQL Server Management Studio 中使用应用程序角色:sp_setapprolesp_unsetapprole 直接但是我在尝试这样做时遇到了问题使用 NHibernate:

using (var session = SessionFactory.OpenSession())
{
byte[] cookie;

// 1) Set the application role
using (var command = session.Connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "sp_setapprole";
command.Parameters.Add(new SqlParameter("@rolename", RoleName));
command.Parameters.Add(new SqlParameter("@password", RolePassword));
command.Parameters.Add(new SqlParameter("@fCreateCookie", true));
command.Parameters.Add(
new SqlParameter
{
ParameterName = "@cookie",
DbType = DbType.Binary,
Direction = ParameterDirection.Output,
Size = 8000
});

// This works perfectly fine
command.ExecuteNonQuery();
var outVal = (SqlParameter)command.Parameters["@cookie"];

// This returns a byte array value for the cookie generated
cookie = (byte[])outVal.Value;
}

// 2) This line dutifully retreives my contrived Person object but renders
// part 3) of this saga next to useless because it performs a logout
// (identified with SQL Profiler) and the app role/cookie is forgotten.
session.CreateCriteria(typeof(Person)).List<Person>().FirstOrDefault();

// 3) Unset the application role
using (var command = session.Connection.CreateCommand())
{
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "sp_unsetapprole";
command.Parameters.Add(new SqlParameter("@cookie", cookie));
command.ExecuteNonQuery();
}
}

总而言之,在上述示例中执行 1) 和 3) 之间的任何操作都会导致应用程序崩溃。以两种不同的方式取决于我是否在池化:

Pooling - 当前命令发生严重错误。结果(如果有)应该被丢弃。

未池化 - 无法取消设置应用程序角色,因为未设置任何角色或 cookie 无效。

我不确定这是否有帮助,但我正在连接以下内容(通常会略有不同,但我需要配置 Pooling 属性):

private static ISessionFactory CreateSessionFactory()
{
return Fluently.Configure().Database(
MsSqlConfiguration.MsSql2008.ConnectionString(
"Data Source=(local);" +
"Initial Catalog=PeopleDB;" +
"Integrated Security=False;" +
"User ID=someuserid;" +
"Password=somepassword" +
"Pooling=False"))
.Mappings(m => m.FluentMappings.AddFromAssemblyOf<Program>())
.BuildSessionFactory();
}

仅供引用,在上述代码中连接的用户绝对没有对数据库的权限,这至少证明 sp_setapprole 正在运行。

有没有人遇到过这个问题并设法解决了?我有一个很好的谷歌,但什么也没找到。

提前致谢,罗布

最佳答案

NH 在需要时使用 IConnectionProvider 创建和关闭连接.如果应用程序中使用的每个连接都使用相同的身份验证过程,那么最好实现您自己的 IConnectionProvider并用 MsSqlConfiguration.MsSql2008.Provider<YourOwnConnectionProvider>() 配置它;

class MyConnectionProvider : DriverConnectionProvider
{
public override IDbConnection GetConnection()
{
var connection = base.GetConnection();

byte[] cooky;
// TODO: authenticate

return new MyConnectionWrapper(connection, cooky);
}

public override void CloseConnection(IDbConnection conn)
{
var myConn = conn as MyConnectionWrapper;
if (myConn != null)
{
// TODO: deregister with myConn.Cooky;
}
base.CloseConnection(conn);
}
}

关于c# - 将 NHibernate 与 SQL Server 应用程序角色一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8506504/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com