我有这个非常基本的MVC4
应用程序通过对象实体模型连接到数据库。应用程序和数据库之间的连接很好,因为用户能够登录并且Global.asax
中的以下代码正在运行(它从数据库中成功检索用户角色。
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;
}
}
错误发生在以下行:
@if(User.IsInRole("Administrator"))
{
<span>testtttt</span>
}
除了上述内容外,我还尝试了以下内容,但仍然遇到了同样的错误:
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
答案 0 :(得分:15)
我之前使用123456789012345678900000000000
12345678901234567890000000000
1234567890123456789000000000
123456789012345678900000000
12345678901234567890000000
1234567890123456789000000
123456789012345678900000
12345678901234567890000
1234567890123456789000
123456789012345678900
12345678901234567890
1234567890123456789
123456789012345678.9
12345678901234567.89
1234567890123456.789
123456789012345.6789
12345678901234.56789
1234567890123.456789
123456789012.3456789
12345678901.23456789
1234567890.123456789
123456789.0123456789
12345678.90123456789
1234567.890123456789
123456.7890123456789
12345.67890123456789
1234.567890123456789
123.4567890123456789
12.34567890123456789
1.234567890123456789
0.1234567890123456789
0.01234567890123456789
0.001234567890123456789
0.0001234567890123456789
0.00001234567890123456789
0.000001234567890123456789
0.0000001234567890123456789
0.00000001234567890123456789
0.000000001234567890123456789
0.0000000001234567890123456789
0.00000000001234567890123456789
但已更改为使用基于SimpleMembership
的{{1}}的应用程序存在同样的问题。将以下内容添加到我的Single Sign-on
为我解决了问题:
Owin Authentication Middleware
答案 1 :(得分:4)
不久前遇到了类似的问题。正在使用多个项目解决方案。能够无问题地进行身份验证的用户;当他们去更新他们的用户档案时,他们会得到你提到的错误。
最后,我们发现Visual Studio已将连接字符串插入到指向开发环境数据库的基础结构项目的app.config中,该数据库无法从生产环境中访问。
我们从App.config中删除了连接字符串设置,重新部署,之后生活很好。
答案 2 :(得分:2)
您的错误明确指出您尝试使用需要它来创建文件并将其附加到服务器的本地SQL Express实例。
您可以查看以下内容:
运行应用程序的帐户是否具有读/写访问权限 有问题的目录? (默认为App_Data)
您是否正确配置了连接字符串?
如果查看InitializeDatabaseConnection
的方法签名(用于设置角色和成员资格提供程序),则为:
public static void InitializeDatabaseConnection(string connectionStringName, string userTableName, string userIdColumn, string userNameColumn, bool autoCreateTables);
连接字符串需要一个“常规”连接字符串,而不是Entity Framework的Db / Model第一个模式生成的连接字符串。
我还注意到您正在使用用户test.user
。 SimpleMembershipProvider
和SimpleRoleProvider
使用字符串连接来构建查询。你在那里的时期可能会导致问题。你可以在没有这段时间的情况下进行测试吗?
答案 3 :(得分:1)
看起来您正在使用ASP.NET成员资格提供程序,它可以指定不同的连接字符串。验证您是否指向正确的连接字符串。 见Configuring an ASP.NET Application to Use Membership
答案 4 :(得分:1)
尝试在两种情况下打破GetSqlConnection(...)以查看传递的参数。
答案 5 :(得分:1)
如果您未在machine.config
中指定,则您正在使用的角色提供程序将从web.config
文件中获取配置。这将导致您的应用在LocalSql
中创建App_Data
数据库。连接字符串和提供商详细信息在machine.config
中指定,因此您的web.config
只会添加或删除它们。例如,您的machine.config
可能会说:
<connectionStrings>
<add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>
你的web.config
有这个:
<connectionStrings>
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
您的应用程序将能够查看和使用两个字符串。要修复它,您可以首先清除连接字符串,如下所示:
<connectionStrings>
<clear />
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
但这仍然存在需要在web.config
中设置您的角色提供程序的根本问题。为此,请添加以下内容:
<system.web>
<roleManager>
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" connectionStringName="<your connection string name here>" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />
</providers>
</roleManager>
</system.web>
此处的关键部分可能只是</clear>
行,它会删除machine.config
继承中的所有内容。或者,您可以像这样指定默认提供程序:
<roleManager defaultProvider="MyProvider">
<providers>
<add name="MyProvider" ......>
...etc...