Database permissions in MVC with NHibernate

爱⌒轻易说出口 提交于 2019-12-12 04:41:07

问题


I'm working on an intranet MVC web application, using Fluent NHibernate.

As everyone knows, creating the necessary ISessionFactory is heavy, and therefore should be done only once. Hence, I create it in the Global.asax file during Application_Start, then cache it in the application for future use.

The problem is that I only want to give access to users who already have permissions over the database.

This could theoretically be solved by defining Integrated Security=SSPI in the connection string (rather than by providing an SQL username and password).

However, this raises an error during Fluently.Configure, because the configuration occurs during Application_Start, which is being run by the process hosting the application, which does not have permissions to connect to the DB.

How do I solve this issue?


回答1:


You could initialize it in BeginRequest instead of Application_Start:

private static bool _initialized = false;
private static object _syncRoot = new object();

protected void Application_BeginRequest(object source, EventArgs e)
{
    if (_initialized)
    {
        return;
    }

    lock (_initialized)
    {
        if (_initialized)
        {
            return;
        }

        // Initialize the session factory here and cache it

        _initialized = true;
    }
}


来源:https://stackoverflow.com/questions/11119958/database-permissions-in-mvc-with-nhibernate

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!