I am implementing in a development project Oracle Session State Store Class, which is found in Oracle 11g Client to hold my users Sessions as the application is being run over a web farm.
Whilst I can make Oracle Session State Store work through the web.config, I need to make the store work in the code behind because our database connections i.e. the passwords are encrypted and are only decrypted when a connection is made. Unfortunately, I cannot do this in the web.config.
I have tried to implement the Oracle Session State Store Class in the Global.asax in the Session_Start method using the Initialise method. The problem is that it is throwing an error. The exception it is calling is "The connectionStringName attribute is empty or does not exist in the configuration file, or an invalid attribute is found in the configuration file."
In the Session_Start method, I created a Name Value Collection and added two variables, one of which is the database connection string and the other the type for the custom session store.
Does anyone know what I am doing wrong?
Thanks
First, make sure your web.config or machine.config is configured correctly. You can take a look at Oracle Session State Store documentation, or below:
<?xml version="1.0"?>
<configuration xmlns=
"http://schemas.microsoft.com/.NetConfiguration/v2.0">
<connectionStrings>
<add name="my_sessionstate_app_con_string" connectionString=
"User Id=scott;Password=tiger;Data Source=Oracle"/>
</connectionStrings>
<system.web>
<!-- Enable and customize OracleSessionStateProvider -->
<sessionState mode="Custom" customProvider="MyOracleSessionStateStore">
<providers>
<add name="MyOracleSessionStateStore"
type="Oracle.Web.SessionState.OracleSessionStateStore,
Oracle.Web, Version=2.111.6.20, Culture=neutral,
PublicKeyToken=89b483f429c47342"
connectionStringName="my_sessionstate_app_con_string"/>
</providers>
</sessionState>
</system.web>
</configuration>
Second, you don't HAVE to encrypt your Oracle session state store connection string during development time. You can use aspnet_regiis to encrypt it at deployment like below.
aspnet_regiis -pef connectionStrings "c:\inetpub\wwwroot\myapp\"
If the command line above runs fine but your web app produces RSA error messages, try to add the following accounts (or the specific ones you use) to the RSA container:
aspnet_regiis -pc "NetFrameworkConfigurationKey" -exp
aspnet_regiis -pa "NetFrameworkConfigurationKey" "ASPNET"
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NETWORK SERVICE"
aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY\NETWORK SERVICE"
By the way, I didn't have to implement OracleSessionStateStore in Global.ascx. I just reference with Session["key"] = value; and var value = Session["key"];
Also, make sure your DBA setup a job to clean up the session state table.
Related
We're trying to leverage Azures deployment slots for an Umbraco site we've built.
By default Umbraco uses a DSN defined in the connectionStrings sections of the web.config and we want it to use the connection string for the deployment slot it's sitting in.
What we've tried
Azure deployment slots put all defined app settings (and connection strings) into environment variables and to access them we can use Environment.GetEnvironmentVariable() which works but there doesn't seem to be a way to tell Umbraco to do this.
So in OnApplicationInitialized() (in /App_Code/Core/UmbracoAppStart.cs) we loaded the connectionstring section from the web.config, grabbed the connstr from env vars, added the DSN to the connectionstring section and saved.
The correct connection string is grabbed and stored but this seems to recycle the app (due to a web.config change) and thus we just get timeouts. (Or Umbraco XML cache errors, or it takes 20 mins to load the page).
I know you can store the appsettings and connectionstrings sections in separate files. But the file attribute (that doesn't cause a recycle if the referenced file is changed) doesn't work on the connectionStrings section - only the configSource attribute and that DOES recycle if changed.
(from: ASP.NET web.config: configSource vs. file attributes)
Help
Has anyone found a way around this?
We simply need to get Umbraco to use the deployment slot connection string - not the one in webconfig.
I'm even willing to copy and paste blindly at the moment without understanding how it works - and I hate doing that :). But that's what happens when people agree when the client wants to go live just before Christmas...
You don't need to do any code to use Azure connection string or the app settings. Just give them the same keys/names as you have on your web.config and they will be used instead.
So if you have this on your web config:
<add name="umbracoDbDSN" connectionString="Server={server};Initial Catalog={db};Persist Security Info=False;User ID={user};Password={password};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=300;" providerName="System.Data.SqlClient" />
Your slot configuration should be this:
To replace an app setting just use the same key. So for this:
<add key="umbracoUseSSL" value="false" />
You'd use this:
If you want the setting to be slot specific you have to activate the Slot setting checkbox.
I have an MVC4 application with an aspnet.mdf database with membership tables and I’ve added two more tables to it. Locally I’m using “AccountModels.cs” to handle membership and a Database first EDMX modal to handle the two other tables. So I have two connection strings that are working great locally.
Remotely, my Database first EDMX modal "aspnetEntities" connection string works fine but I don’t know what I need to do to get the "DefaultConnection" connection string to work, this is what I had hoped would work:
Remote DefaultConnection not working
<add name="DefaultConnection" connectionString="Data Source=mydatabase.db.3730456.hostedresource.com; Initial Catalog=mydatabase; Persist Security Info=True; MultipleActiveResultSets=True; User ID=mydatabase; Password=mypass" providerName="System.Data.SqlClient"/>
Locally it looks like this, but it's generated for me when I add the EDMX
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\aspnet.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
And my working remote aspnetEntities connection string
<add name="aspnetEntities" connectionString="metadata=res://*/Models.RegisterModel.csdl|res://*/Models.RegisterModel.ssdl|res://*/Models.RegisterModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=mydatabase.db.3730456.hostedresource.com; Initial Catalog=mydatabase; User ID=mydatabase; Password='mypass'"" providerName="System.Data.EntityClient" />
When I try to run the membership part of the site remotely, I get an error saying it’s trying to build a new database,
CREATE DATABASE permission denied in database 'master'.
Can someone help me out with a connection string that will work?
Never mind, the answer is to not use the default membership provider and build your own custom one. Aspnet.mdf is way too bloated anyway!
How do you change an MVC3 web app to use a remote server for the data context? I tried changing the connection string the in web config (and restarted everything) but it still uses the local SQL database.
Edit: my connection string is below
<add name="ApplicationServices" connectionString="Data Source=zzz.database.windows.net;Initial Catalog=SMS;Persist Security Info=True;User ID=xyz;Password=abc" providerName="System.Data.SqlClient" />
Edit 2: I tried commenting out everything inside the <connectionStrings> tag. The site still works. I'm a bit confused, is the connection set somewhere else?
If you are using Entity Framework code first (which comes with asp.net mvc 3), the name of the database class corresponds with the name of the connection string. For example if you have a class like this:
public class DataContext : System.Data.Entity.DbContext
{
[...]
}
The name of your connection string should also be DataContext. More info here at step 4.
You change the connection string in your web.config. For example if you are using a SQL server your connection string could look like this:
<add name="ApplicationServices"
connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;"
providerName="System.Data.SqlClient" />
where Data Source points to the name of the remote SQL server, Initial Catalog the name of the database to connect to and User Id and Password seem pretty self explanatory.
Now obviously in your code you will use the ApplicationServices connection string somewhere, so depending on what data access you are using or whether you are using an ORM there might be different places to look for. So if despite changing the connection string in your web.config your code continues to use a local database, well, it's obvious that this is hardcoded somewhere in your code.
I've read about the New Membership and Session providers, and the information in this article is that just changing the connectionStrings the database would build itself magically in my SQL Azure Database.
So, I first changed my connection to:
<add name="Project" connectionString="Server=tcp:xxxxxxxx.database.windows.net,xxxx;Database=xxxxx;User ID=xxxxxxxxxxx;Password=xxxxxxxxxxx;Trusted_Connection=False;Encrypt=True;" providerName="System.Data.SqlClient" />
When I tried to debug my app with the emulator, I got the error:
EFProviders require MultipleActiveResultSets=True for System.Data.SqlClient connection strings.
I researched about this error and realized that it was something related to Entity Framework. I tried to make an Entity Framework connection string with no success. In addtion, I read somewhere that SQL Azure doesn't support MultipleActiveResultSets.
So, I have 2 questions:
Is it true that if I run the application and register any user via website interface the Membership and Session tables, views and procedures will magically build in my SQL Azure database?
What am I missing to make my app run?
Detail: I didn't add any new item in Models folder. It's only AccountModels class, as it generates from MVC3 template.
Fortunatelly now I have the 2 answers:
Yes, it's true and very easy!
The mistake I did first time it was try to put MultipleActiveResultsSet as a new attribute in the connectionString line. The code have to be like this:
<add name="Project" connectionString="Server=tcp:xxxxxxxx.database.windows.net,xxxx;Database=xxxxx;User ID=xxxxxxxxxxx;Password=xxxxxxxxxxx;Trusted_Connection=False;Encrypt=True;**MultipleActiveResultSets=True**" providerName="System.Data.SqlClient" />
Edited
Now I found out other way to do that with AppFabric Caching for Session State. Just follow this tutorial.
When using sqlexpress
<add name="SchoolContext"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=CatomMvc;Integrated Security=True"
providerName="System.Data.SqlClient" />
I get an error
Model compatibility cannot be checked because the database does not
contain model metadata. Ensure that IncludeMetadataConvention has been
added to the DbModelBuilder conventions.
When I change to
<add name="SchoolContext" connectionString="Data Source=|DataDirectory|School.sdf" providerName="System.Data.SqlServerCe.4.0"/>
It's work but I want to use sqlexpress or sql 2008 that gave the same error.
The database CatomMvc may already exist in .\sqlexpress.
Try to rename the database to something like Catomvc_backup or drop the database, then try again.
Alternatively you could use the following within your Application_Start():
Database.SetInitializer(new DropCreateDatabaseAlways<YourContext>());
This will force the Database to be recreated, but generally during development you should only use:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<YourContext>());
To allow the Database to be recreated based on changes to your model.
However, neither of these should make it into your production code!
Just make sure your Database.SetInitializer function is at the top of your Application_Start function.
Does the trick :)
Sometime it happens because you dont have sysadmin role on local SQL Server instance. I found useful article where you can download script command and install it. You will be ask for SQLEXPRESS name or instance name, just you can write 'SQLEXPRESS' and enter. Thats it. It will add you to sysadmin role of local server instance.
Download command from: http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=addselftosqlsysadmin&DownloadId=9198
Please let me know if it does not work for you.