ASP.NET 角色管理器错误

4

我试图在我的应用程序中使用角色来隐藏元素,在我的开发环境中的Visual Studio Express中这个功能运行良好。但是当我发布网站并将其放入实际环境中时,我现在会收到一个关于无法连接到SQL Server实例的错误。

看起来好像有几个人在这篇文章中评论了同样的事情:The Role Manager feature has not been enabled

但是我没有看到对该评论的解决方案。

如果我在web.config文件中取出roleManager enabled="true"这一行代码,那么网站就会连接,直到我有代码要隐藏控件,然后就会出现关于rolemanager未启用的错误。

这是我的web.config文件,我不知道如何使其正常工作。对于很多Web编程的新手来说仍然是一个难题:

<?xml version="1.0" encoding="utf-8"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=S192-169-136-24\ABPATH;Initial Catalog=aspnet-cs1-20160511031253;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="CS1" connectionString="Data Source=S192-169-136-24\ABPATH;Initial Catalog=CS1;Integrated Security=True" providerName="System.Data.SqlClient" />
 </connectionStrings>
 <system.web>
 <authentication mode="None" />
 <compilation targetFramework="4.5.2" />
 <httpRuntime maxRequestLength="2097151" targetFramework="4.5.2" requestValidationMode="2.0" requestPathInvalidCharacters="" />
  <pages validateRequest="false">
  <namespaces>
    <add namespace="System.Web.Optimization" />
    <add namespace="Microsoft.AspNet.Identity" />
  </namespaces>
  <controls>
    <add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt" />
  </controls>
</pages>
<membership>
  <providers>
    <!--
      ASP.NET Membership is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
    -->
    <clear />
  </providers>
</membership>
<profile>
  <providers>
    <!--
      ASP.NET Membership Profile is disabled in this template. Please visit the following link http://go.microsoft.com/fwlink/?LinkId=301889 to learn about the ASP.NET Membership support in this template
    -->
    <clear />
  </providers>
</profile>
<roleManager defaultProvider="DefaultConnection" enabled="true">
<providers>
   <add name="DefaultConnection" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=NUMBER" connectionStringName="DefaultConnection" applicationName="/" />
 </providers>
</roleManager>
<!--
        If you are deploying to a cloud environment that has multiple web server instances,
        you should change session state mode from "InProc" to "Custom". In addition,
        change the connection string named "DefaultConnection" to connect to an instance
        of SQL Server (including SQL Azure and SQL  Compact) instead of to SQL Server Express.
  -->
  <sessionState mode="InProc" customProvider="DefaultSessionProvider">
  <providers>
    <add name="DefaultSessionProvider" type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" />
  </providers>
 </sessionState>
  <httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
 </httpModules>
 </system.web>
 <system.webServer>
 <rewrite>
    <rules>
    <rule name="HTTP to HTTPS redirect" stopProcessing="true"> 
    <match url="(.*)" /> 
    <conditions> 
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
    </conditions> 
    <action type="Redirect" redirectType="Permanent" url="https://{HTTP_HOST}/{R:1}" />
  </rule>   
    </rules>
  </rewrite>
  <modules>
  <remove name="FormsAuthentication" />
  <remove name="ApplicationInsightsWebTracking" />
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" preCondition="managedHandler" />
</modules>
<validation validateIntegratedModeConfiguration="false" />
    <httpRedirect enabled="false" destination="https://www.pathcertlink.com/" exactDestination="true" />
</system.webServer>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
  <dependentAssembly>
    <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="WebGrease" culture="neutral" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-1.5.2.14234" newVersion="1.5.2.14234" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" />
    <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin" culture="neutral" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security.OAuth" culture="neutral" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
  </dependentAssembly>
  <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security.Cookies" culture="neutral" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
    </dependentAssembly>
    <dependentAssembly>
    <assemblyIdentity name="Microsoft.Owin.Security" culture="neutral" publicKeyToken="31bf3856ad364e35" />
    <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
    </dependentAssembly>
    </assemblyBinding>
   </runtime>
 <entityFramework>
  <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
   <providers>
  <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
   </providers>
  </entityFramework>
  <system.codedom>
 <compilers>
  <compiler language="c#;cs;csharp" extension=".cs" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:6 /nowarn:1659;1699;1701" />
  <compiler language="vb;vbs;visualbasic;vbscript" extension=".vb" type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider, Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" warningLevel="4" compilerOptions="/langversion:14 /nowarn:41008 /define:_MYTYPE=\&quot;Web\&quot; /optionInfer+" />
  </compilers>
 </system.codedom>
 </configuration>

你确定问题是由于角色提供程序吗?看起来错误是因为在连接字符串中使用了Windows身份验证:“Integrated Security=True”。所以你应该先在<system.web>下启用模拟 <identity impersonate="true" />。请参阅如何:使用 Windows 集成安全性访问 SQL Server - Reza Aghaei
@RezaAghaei 所涉及的应用程序是在互联网上,而不是内部网络,但您可能会发现开发环境显然是内部网络。我对创建Web应用程序非常陌生(这是我的第一个),因此更改web.config以与Internet配合使用对我来说是新的。应用程序的用户仅登录应用程序,他们与组织没有任何其他关联,因此根本没有Windows帐户。如果我需要更改web.config才能正常工作,那就是我需要知道如何做的事情。 - Brent Oliver
您正在使用“Windows身份验证”来进行“DefaultRoleProvider”。这意味着执行应用程序的帐户(即应用程序池标识或模拟用户)应该使用Windows身份验证访问数据库。否则,您应该将连接字符串更改为SQL Server身份验证。对于远程数据库,如果您想要使用Windows身份验证,您应该拥有一个可以访问远程数据库的域用户帐户。对于本地数据库(与应用程序服务器机器相同的机器),您应该拥有一个可以访问数据库的本地用户帐户。 - Reza Aghaei
这些是必要的问题:1) 您使用本地数据库(在同一台机器上安装的SQL)还是数据库是远程的(在不同的机器上安装的SQL Server)? 2) 应用程序服务器(和数据库服务器)是否加入了域? 3) 您是否有域用户名/密码或SQL Server用户名/密码来连接数据库? 4) 您的应用程序中采用什么身份验证机制?您使用表单身份验证、Windows身份验证还是其他身份验证方式? - Reza Aghaei
3个回答

4

我认为服务器不叫做LAPTOP-168YQ32

你可能需要将其更改为localhost或主机提供的IP地址。我还认为它不会使用集成安全性,而是需要用户名/密码组合。

连接字符串应该类似于这样。

<add name="DefaultConnection" connectionString="Data Source=192.168.0.101;Initial Catalog=aspnet-cs1-20160511031253;User ID=userName;Password=passWord" providerName="System.Data.SqlClient"/>

尝试更改为User ID=userName;Password=passWord,现在没有人可以登录到网站,因为“userName”的登录失败。知道这一定代表某些简单的东西,但不知道应该是什么。 - Brent Oliver
此外,Web配置文件是来自我的开发环境,实际网站上的连接字符串指向IP地址的生产服务器,但它确实配置了如上所示的集成安全性。 - Brent Oliver
检查SQL服务器的配置,看它是否接受用户名/密码组合?而且你的网站是否托管在公司内部服务器上,因为如果不是,集成安全性就不是一个真正的选择。 - VDWWD

1
角色基础认证是关于在网站上验证用户身份,而不是关于SQL Server连接的,所以我建议从这个问题中删除这种复杂性。你的问题是无法连接到SQL Server - 简单明了。
为了解决这个问题,请检查你收到的“无法连接到SQL Server”的具体错误信息 - 连接被拒绝吗?(这意味着SQL Server没有侦听TCP/1433,而只使用套接字或临时文件)。很可能,密码是不正确的。在远程服务器上,尝试使用RDP(远程桌面)来验证你是否可以使用登录时使用的帐户的用户名和密码登录到SQL Server。最好使用远程服务器上的简单用户名和密码,而不要麻烦地使用集成安全性,通常这只在开发中使用。

这就是我无法理解的地方。该应用程序正在运行,并且显然正在读写SQL数据库,直到我启用角色管理器为止。我不知道其中有什么独特之处,也不知道为什么它无法连接。 - Brent Oliver
我认为启用“rolemanager”,然后添加“Integrated Security”(它获取当前用户ID并使用其登录到SQL Server)可能会导致SQL Server登录失败,这对我来说是有道理的。虽然我不理解内部原理-实际上您已经在ASP.net/IIS中更改了用户。我建议在此生产/实时主机上向SQL服务器添加用户名和密码,并忘记关于“Integrated Security”。 - Charlie Dalsass

1

你的角色管理器部分看起来有些奇怪。汇编令牌中的“NUMBER”是什么意思?

此外,可能是一个细节,在这个阶段值得尝试,给角色管理器指定一个不同于“DefaultConnection”的名称,因为它也是连接的名称。

请尝试以下操作:

<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/"/>
  </providers>
</roleManager>

我看到你的连接中有两个连接。
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=S192-169-136-24\ABPATH;Initial Catalog=aspnet-cs1-20160511031253;Integrated Security=True" providerName="System.Data.SqlClient" />
<add name="CS1" connectionString="Data Source=S192-169-136-24\ABPATH;Initial Catalog=CS1;Integrated Security=True" providerName="System.Data.SqlClient" />
 </connectionStrings>

我猜您的应用程序正在使用连接“CS1”,而Rolemanager使用指向另一个数据库(“aspnet-cs1-20160511031253”)的“DefaultConnection”。
您是否在线上拥有该数据库:aspnet-cs1-20160511031253?如果没有,请添加它,或在CS1中添加aspnet表并将角色管理器更改为指向CS1数据库而不是DefaultConnection。
<roleManager defaultProvider="DefaultRoleProvider">
  <providers>
    <add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="CS1" applicationName="/"/>
  </providers>
</roleManager>

这实际上是之前评论中提到的一些内容,已被恢复为其原始值。应用程序可以连接并正常使用 SQL Server,直到启用角色管理器角色为止。然后它会有问题,但只有角色管理器角色。这很奇怪,因为角色还用于在其他地方隐藏/显示菜单项,而不使用角色管理器。 - Brent Oliver
好的,我编辑了答案,问题可能是您正在使用错误的连接(DefaultConnection而不是CS1),请参见答案底部。 - Luc Debliquis

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接