C# 我能';不要使用角色而不失败&引用;出现与网络相关或特定于实例的错误;

C# 我能';不要使用角色而不失败&引用;出现与网络相关或特定于实例的错误;,c#,asp.net,authentication,web-config,authorization,C#,Asp.net,Authentication,Web Config,Authorization,嗨,我第一次尝试使用和理解角色。 我如何解决这个问题?是否需要向网络配置添加内容 private static void CreateRoleIfNotExists(string role) { if (!Roles.RoleExists(role)) // this line throws the error. { Roles.CreateRole(role); } } 更新,在下面插入完整的stacktrace: [SqlException(0x

嗨,我第一次尝试使用和理解角色。 我如何解决这个问题?是否需要向
网络配置添加内容

private static void CreateRoleIfNotExists(string role)
{
    if (!Roles.RoleExists(role)) // this line throws the error.
    {
        Roles.CreateRole(role);
    }  
}
更新,在下面插入完整的stacktrace:

[SqlException(0x80131904):建立到SQL Server的连接时发生与网络相关或特定于实例的错误。找不到或无法访问该服务器。请验证实例名称是否正确,以及SQL Server是否配置为允许远程连接。(提供程序:SQL网络接口,错误:26-定位指定的服务器/实例时出错)]
System.Data.SqlClient.SqlInternalConnection.OnError(SqlException异常,布尔breakConnection)+4856727
System.Data.SqlClient.TdsParser.throweexception和warning(TdsParserStateObject stateObj)+194
System.Data.SqlClient.TdsParser.Connect(ServerInfo ServerInfo,sqlinternalconnectionds connHandler,Boolean ignoresnopentimeout,Int64 timerExpire,Boolean encrypt,Boolean trustServerCert,Boolean integratedSecurity,SqlConnection owningObject)+4867325
System.Data.SqlClient.sqlinternalconnectionds.AttemptOneLogin(ServerInfo ServerInfo,String newPassword,Boolean ignoresnopentimeout,Int64 timerExpire,SqlConnection owningObject)+90
System.Data.SqlClient.SqlInternalConnectionDS.LoginOfAliver(字符串主机、字符串新密码、布尔重定向声明、SqlConnection owningObject、SqlConnectionString连接选项、Int64 timerStart)+374
System.Data.SqlClient.sqlinternalconnectionds.OpenLoginEnlist(SqlConnection-owningObject、SqlConnectionString-connectionOptions、String-newPassword、Boolean-redirectedUserInstance)+225
System.Data.SqlClient.SqlInternalConnectionDS..ctor(DbConnectionPoolIdentity,SqlConnectionString connectionOptions,Object providerInfo,String newPassword,SqlConnection owningObject,Boolean redirectedUserInstance)+189
System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions选项、对象池组ProviderInfo、DbConnectionPool池、DbConnection owningConnection)+4868451
System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnection所有者连接、DbConnectionPool池、DbConnectionOptions选项)+31
System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject)+431
System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject)+66
System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject)+499
System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection-owningConnection)+65
System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection,DbConnectionFactory connectionFactory)+117
System.Data.SqlClient.SqlConnection.Open()+122
System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext上下文,布尔值revertImpersonate)+87
System.Web.DataAccess.SqlConnectionHelper.GetConnection(字符串connectionString,布尔值还原模拟)+221
System.Web.Security.SqlRoleProvider.RoleExists(字符串roleName)+478
System.Web.Security.Roles.RoleExists(字符串roleName)+73

dk.certifikat.Global.CreateRoleIfNotExists(字符串角色)

如果使用标准的.net成员资格提供程序和roleproviders,则需要设置aspnetdb

这可以通过命令提示符中的命令来完成

aspnet_regsql

这可以在.net framework目录中找到。 如果执行了此操作,则需要为应用程序提供到运行aspnetdb的数据库的连接字符串


有关此问题的详细信息,请参见。

我也遇到了此错误。我曾遵循过他们也使用Roles.RoleExists的示例。当我使用Visual Studio Express在本地运行应用程序时,一切正常。但当我尝试通过web访问应用程序时,我遇到了相同的连接错误

结果我不得不更改web.config文件。 我更改了DefaultProfileProvider、DefaultMembershipProvider和DefaultRoleProvider。我将ConnectionString属性更改为已添加到ConnectionString的属性,即:

<add name="WingtipToys" providerName="System.Data.SqlClient" connectionString="Server=.\SQLEXPRESS;Database=wingtiptoys;Integrated Security=False;User Id=wingtiptoys;Password=wingtiptoys;AttachDbFileName=|DataDirectory|wingtiptoys.mdf;Persist Security Info=False" />

例如,DefaultMembershipProvider现在看起来像:

<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="WingtipToys" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />

使用connectionStringName=“WingtipToys”而不是connectionStringName=“DefaultConnection”


然后一切都很顺利。这可能不是最好的解决方案,但我希望这个答案能帮助别人。

您的数据库已关闭,或被防火墙阻止。@dbaseman我在登录之前不使用任何数据库。我是否缺少数据源(不使用的数据库)?检查您的Web.Config,即成员资格提供程序的配置datasource@Massanu我没有。它应该是什么样子?谢谢,我想我明白你的意思了。我想知道我是否走错了路,做错了。我会重新考虑的。