User.IsInRole扔了一个具体的实例与网络相关的或错误 [英] User.IsInRole throwing a A network-related or instance-specific error

查看:224
本文介绍了User.IsInRole扔了一个具体的实例与网络相关的或错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个很基本的 MVC4 通过对象实体模型conencted到一个数据库应用程序。应用程序和数据库之间的连接是好的,因为用户能够登录及以下code在的Global.asax 在工作(以检索用户角色成功地从数据库中。

I have this very basic MVC4 application conencted to a database via Object Entity Model. The connection between the application and database is fine, because a user is able to login and the below code in the Global.asax is working (which retrieves the user roles successfully from the database.

protected void Application_AuthenticateRequest(object sender, EventArgs args)
    {
        if (User != null)
        {

            IEnumerable<CommonLayer.Roles> roles = new BusinessLayer.Roles().getAllUserRoles(Context.User.Identity.Name);

            string[] rolesArray = new string[roles.Count()];
            for (int i = 0; i < roles.Count(); i++)
            {
                rolesArray[i] = roles.ElementAt(i).RoleName;
            }

            GenericPrincipal gp = new GenericPrincipal(Context.User.Identity, rolesArray);
            Context.User = gp;
        }
    }

该错误是发生在以下行:

The error is occuring on the following line:

@if(User.IsInRole("Administrator"))
    {
       <span>testtttt</span>
    }

除了上述情况,我也试过以下,但仍得到了同样的错误:

Instead of the above, I also tried the following but still got the same error:

Roles.AddUserToRole("test.user","Administrator");

确切的错误是:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

堆栈跟踪:

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)]
   System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +5340635
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +244
   System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +5350895
   System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +145
   System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +922
   System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +307
   System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +518
   System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +5353705
   System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions) +38
   System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +5355906
   System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +146
   System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +16
   System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +94
   System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +110
   System.Data.SqlClient.SqlConnection.Open() +96
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +75

[HttpException (0x80004005): Unable to connect to SQL Server database.]
   System.Web.Management.SqlServices.GetSqlConnection(String server, String user, String password, Boolean trusted, String connectionString) +130
   System.Web.Management.SqlServices.SetupApplicationServices(String server, String user, String password, Boolean trusted, String connectionString, String database, String dbFileName, SqlFeatures features, Boolean install) +89
   System.Web.Management.SqlServices.Install(String database, String dbFileName, String connectionString) +27
   System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName, String dataDir, String connectionString) +386

推荐答案

您的错误明确规定您尝试使用需要它来创建和附加文件到服务器的本地SQL防爆preSS实例。

Your error specifically states you tried to use a local SQL Express instance that requires it to create and attach the file to the server.

下面有一些事情你可以检查:

Here are some things you can check for:


  • 是否运行应用程序的帐户具有读/写访问
    有问题的目录? (默认为App_Data文件)

  • Does the account running the application have Read/Write access to the directory in question? (Defaults to App_Data)

你有你的连接字符串配置正确?

Do you have your connection strings correctly configured?

如果你看一下方法签名 InitializeDatabaseConnection (这是用来设置的作用和成员提供商),它是:

If you look at the method signature for InitializeDatabaseConnection (which is used to setup the role and membership providers), it is:

public static void InitializeDatabaseConnection(string connectionStringName, string userTableName, string userIdColumn, string userNameColumn, bool autoCreateTables);

连接字符串需要一个正规的连接字符串,而不是一个由实体框架的分贝生成/型号第一图案。

The connection string expects a "regular" connection string, not one generated by Entity Framework's Db/Model first patterns.

我也注意到你正在使用的用户 test.user 。在 SimpleMembershipProvider SimpleRoleProvider 使用字符串连接来构建查询。你在那里有周期可能会造成问题。你可以没有时间测试它?

I also notice you are using the user test.user. The SimpleMembershipProvider and SimpleRoleProvider use string concatenation to build the queries. The period you have in there could be causing the issues. Can you test it without the period?

这篇关于User.IsInRole扔了一个具体的实例与网络相关的或错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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