asp.net change value of a web.config item? - asp.net

im trying to get role provider working in multiple environments and hitting a wall
(link text)
i should be able to dynamically set the connectionString property of the web.congig item on app_onstart to the correct DB conection String to get it to work...
can anyone show me how to dynamically alter items in the web.config?
im guessing reflection...
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<clear/>
<add name="SqlRoleManager" type="System.Web.Security.SqlRoleProvider" connectionStringName="ISConnectionString_prod" applicationName="IS"/>
</providers>
</roleManager>
i want to adjust the connectionStringName value in the above snipet
thanks

If you're using VS2010, you can get it to automatically apply transforms to your config files depending on which environment you're publishing to.
We use this to set connection strings, payment provider config settings (sandbox mode, username, etc.) and a couple of other things like how exceptions are handled.
If you're not publishing, you can actually hook these transforms straight into the build engine by editing the project file.
This makes it incredibly simple to maintain (You have a web.config and a web.Live.config which contains the transforms). It also makes the whole process far less error-prone
eg:
web.config
<connectionStrings>
<clear />
<add name="OurConnectionString" connectionString="Data Source=DevDbHostname;Initial Catalog=DevDb;user id=Username;password=Password;MultipleActiveResultSets=True" />
</connectionStrings>
web.Release.config
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<connectionStrings>
<add name="OurConnectionString"
connectionString="Data Source=LiveDbHostname;Initial Catalog=LiveDb;user id=Username;password=Password;MultipleActiveResultSets=True"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
</configuration>

As long as the permissions allow it (you will have to change them), you can treat the web.config just as any other XML file. Knowing that, you can simply use an XDocument and pop in a new XElement where you want it. But be very careful and be sure to save some backups!

Related

ASP.NET Identity - where my tables created?

I have created sample register page to create user using ASP.NET Identity. Actually, I am working with this project since long back; which means, database is already existing.
From Register.aspx page, when clicked on the Register button, getting the message like "User created successfully"; but could not able to find out where relevant tables created.
Below is the connectionstrings section of my web.config file. Please note that, second connectionString "LocalDBConnectionString" is existing but NOT using anywhere, and in-fact not working.
I have verified with all the relevant databases for membership tables, but could not find relevant one.
<connectionStrings>
<add name="ASHOKConnectionString" connectionString="Data Source=SHRESTASOFT\SQLEXPRESS;Initial Catalog=ASHOK;Persist Security Info=True;User ID=sa; Password=tentod" />
<add name="LocalDBConnectionString" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ClassDB.mdf;Integrated Security=True" />
<add name="ASHOKEntities" connectionString="metadata=res://*/EntityFramework.ASHOK.csdl|res://*/EntityFramework.ASHOK.ssdl|res://*/EntityFramework.ASHOK.msl;provider=System.Data.SqlClient;provider connection string="data source=SHRESTASOFT\SQLEXPRESS;initial catalog=ASHOK;persist security info=True;user id=sa;password=tentod;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
<add name="NORTHWINDDataContext" connectionString="metadata=res://*/EntityFramework.NorthwindDataContext.csdl|res://*/EntityFramework.NorthwindDataContext.ssdl|res://*/EntityFramework.NorthwindDataContext.msl;provider=System.Data.SqlClient;provider connection string="data source=SHRESTASOFT\SQLEXPRESS;initial catalog=NORTHWND;persist security info=True;user id=sa;password=tentod;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
Where could the database be created?
Update - One more help required: Please observe that ASHOKConnectionString is existing inside connectionstrings section of web.config. This is the one I am using currently for other pages. If I want to get my ASP.NET Identity related tables into this database, how can I do? Can anybody please suggest me!
You can find where the members are stored by looking the membership field on web.config. For example you have to see something like:
<roleManager ... >
<providers>
<clear/>
<add connectionStringName="ASHOKConnectionString" ... />
</providers>
</roleManager>
<membership >
<providers>
<clear/>
<add connectionStringName="ASHOKConnectionString" ... />
</providers>
</membership>
and inside that database that is used for members, there are 11 tables that starts with aspnet_ and there are your member.

ASP.NET MembershipProvider membership info (usernames, passwords) not being stored? How would I implement this system in a networked environment?

I am working on a project in ASP.NET using MembershipProvider for my login system. Our issue is that the ASPNETDB file generated by ASP.NET upon creation of the login system seems to be empty or is not properly storing member information. Our project is a Web Site project, and we ran into a weird case of not being able to maintain login info. I gave my colleague my ASPNETDB file, he overwrote his and yet the login system was still using his old member information, it would not recognize the new ASPNETDB file. So I'm assuming the issue is that the member information is not being stored in that specific file as I was expecting.
How do we remedy this? We need to install this system for a client, so we will probably host the site sometime soon, how do we localize the membership information so that it's consistent across multiple workstations? Right now the membership info seems to be tied to specific computers. And when we go into the ASPNETDB file there doesn't seem to be anything there.
Can anyone shine some light on this? Its been happening for a while now.
I still don't know how to make it so that it always pulls from the
same location (project directory) instead of SQL server.
In ASP.NET Membership, connection strings for memership and roleManager are in web.config. You just need to update them accordingly.
<configuration>
<connectionStrings>
<remove name="SqlConnection"/>
<add name="SqlConnection" connectionString="..."/>
</connectionStrings>
<system.web>
<membership>
<providers>
<clear/>
<add connectionStringName="SqlConnection" ... />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="SqlConnection" ../>
</providers>
</roleManager>
</configuration>

What does <clear /> signify when specifying a connectionstring?

This answer to another question states:
Do not forget to clear the connectionStrings first:
<connectionStrings>
<clear />
<add name="LocalSqlServer" connectionString="Data Source=(local);Initial Catalog=aspnetdb;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
... interesting. What does that do?
In .Net config files are inherited, so your applications config will inherit settings from your machines config.
The <clear/> tag will remove any inherited connection strings and thereby avoids confusion and potential problems.
In ASP.Net you may have several inherited connection strings, so this is very common there.
The element removes all sections and section groups from your application that were defined earlier in the current configuration file or at a higher level in the configuration file hierarchy.
http://msdn.microsoft.com/en-us/library/aa903345(v=vs.71).aspx
so for example, if this was a child config file and the parent config file had some settings... you may not want them being inherited so you specify the clear flag to clear it and then use your settings.

Can I run multiple websites under a single membership database?

I'm trying to plan a series of websites that all share many of the resources such as css/jscript/images/content etc. For this reason I wanted to run all of the websites under the same application and IIS profile, but depending on the URL being used change the masterpage and theme/skin.
The ASP.NET membership database seems as if it was designed with this goal in mind because it allows you to setup multiple applications, however I believe the purpose for which this was built was to allow applications to be run under virtual directories/folders, not on separate URLs.
Is it possible to map a url to a particular application?
Thanks in advance
Al
Yes it is possible to do this. In your configuration, use the applicationName for your providers. This way, all of the data will be saved in the same database, but kept separate by the Application Id that you will find in most of the tables.
One possibility for your shared resources can be to put them in just one location and you can point to that location from your other site by using a full url to the file in the first location.
Another possibility is to host one app in a virtual directory in the same domain, although you can get into some interesting issues with web.config inheritance doing this. It would help if you show your intended domain naming for the two applications.
In one application:
web.config 1:
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" applicationName="/ApplicationOne"
...add other properties here as standard
/>
</providers>
</roleManager>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" applicationName="/ApplicationOne"
...add other properties here as standard
/>
</providers>
</membership>
In your other application:
web.config 2:
<roleManager enabled="true">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" applicationName="/ApplicationTwo"
...add other properties here as standard
/>
</providers>
</roleManager>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" applicationName="/ApplicationTwo"
... add other properties here as standard
/>
</providers>
</membership>
The easiest solution would be to include the style sheet depending on what URL the page is being executed on, using:
Request.ServerVariables("SERVER_NAME")
IE (pseudo):
if Request.ServerVariables("SERVER_NAME") = "http://www.domain1.com" then
include stylesheet1
else
include stylesheet2
end if
You would need to find a function to extract the domain name from the URL so it works well.

Godaddy ASP.NET membership database woes

I purchased a Windows shared hosting account on godaddy that came with 2 MSSQL databases. I setup one to hold my site data and the other installed aspnet membership schema to store site members. The site works perfectly even displaying data from the 1st database. However when I try to login or register I get this nasty error
Exception Details:
System.Configuration.Provider.ProviderException:
The SSE Provider did not find the
database file specified in the
connection string. At the configured
trust level (below High trust level),
the SSE provider can not automatically
create the database file.
Ive gone through my web.config and there's nothing wrong with my 2 connection strings. It seems godaddy has a problem with using 2 mssql databases simultaneously when 1 is for membership.
Does anyone know a solution or a workaround?
I hope my experience benefits every one. Basically if you want to avoid aspnet membership problems on godaddy always use "LocalSqlServer" as the connectionstring. i.e
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider" connectionStringName="LocalSqlServer"
..other attributes here... />
</providers>
Then create the "LocalSqlServer" connectionString...remember to remove it first
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer"
connectionString="Data Source=xxxx; Initial Catalog=xxx; User ID=xxx; Password=xxx;"
providerName="System.Data.SqlClient" />
</connectionStrings>
I ran into same problem and am using MVC3. Above solution works but with some other changes in MVC3. It took me long time to figure it out so if anybody has similar issue in MVC3 it might help them:
Search for "connectionStringName" in web.config and replace the name with connectionStringName="LocalSqlServer"
Also under connectionstrings make sure
-To add (As this is important for all who are using shared hosting it will replace machine.config LocalSqlServer connectionstring with yours.)
-Keep your current connectionstring (In my case it is FilmiDb, this is required for you to connect to the database in EF model. Without this you will get errors.)
<connectionStrings>
<remove name ="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Initial Catalog=SofilmiDb;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
<add name="FilmiDb" connectionString="Data Source=.\SQLExpress;Initial Catalog=FilmiDb;Integrated Security=SSPI" providerName="System.Data.SqlClient" />
</connectionStrings>

Resources