p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Professional (http://p2p.wrox.com/forumdisplay.php?f=215)
-   -   error in web.config when using roles (http://p2p.wrox.com/showthread.php?t=66346)

hertendreef March 3rd, 2008 03:43 AM

error in web.config when using roles
 
Hi,

When the application doesn't use Roles, this configuration (web.config) works:

<configuration>
<connectionStrings>
<clear/>
<add name="myconn" connectionString="Data Source=.\sqlexpress;Initial Catalog=mydb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

...
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="myconn" />
</providers>
</membership>
...

When i use Roles with this web.config:
------------------------------------------------------
<configuration>
<connectionStrings>
<clear/>
<add name="myconn" connectionString="Data Source=.\sqlexpress;Initial Catalog=mydb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
     <authorization>
      <allow roles="role1"/>
      <allow roles="role2"/>
     </authorization>
    <roleManager enabled="true">
      <providers>
      </providers>
    </roleManager>
...
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider"/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="myconn" />
</providers>
</membership>
...

I get this error: "The connection name 'LocalSqlServer' was not found in the applications configuration or the connection string is empty"
line 149: <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" ...

Source File: C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Conf ig\machine.config line 149

I solved this by adding in web.config this line: <add name="LocalSqlServer" connectionString="Data Source=.\sqlexpress;Initial Catalog=mydb;Integrated Security=True" providerName="System.Data.SqlClient"/>

But i would like understand what happened.
Why do i have to add the second connectionString "LocalSqlServer" only when using Roles? Whats' the meaning of that error in machine.config?

Thanks

Hertendreef


Imar March 3rd, 2008 04:23 AM

Hi there,

The ASP.NET Application Services by default use a connection string called LocalSqlServer that looks for an ASPNETDB.mdf file in the App_Data folder. This means that Membership, Roles and Profile all make use of this connection. In your config file, you changed the behavior for Membership like this:

<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
connectionStringName="myconn" />

With this change, the Membership provider uses the myconn connection string, but the RoleManager doesn't yet. In order to tell the RoleManager to use myconn as well you need to override its settings as well like this:

<roleManager defaultProvider="SqlProvider"
  .. other attributes go here
>
  <providers>
    <add
      name="SqlProvider"
      type="System.Web.Security.SqlRoleProvider"
      connectionStringName="myconn"
  />
  </providers>
</roleManager>

This changes the RoleManager and sets Sqlprovider as the new default provider. Alternatively, you could remove and readd the standard settings for RoleManager, just like you do with Membership. The machine.config file on your machine (at C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONF IG) defines the default behavior for these providers.

Hope this sheds some light....

Imar

---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004

hertendreef March 8th, 2008 05:47 AM

Thanks for replying.
It works now.



All times are GMT -4. The time now is 04:42 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.