0

OK. I am experiencing something very strange with the asp.net identity in web forms. I setup the web.config file to establish connection to my local sqlexpress server. I previously created a database and ran the web application which uses asp.net identity and the 4.5 .net framework. The first time I ran the web app the tables were automatically created for me in the sql server database but then I noticed that in the APP_DATA folder under the solution there was another database created with a name aspnetdb.mdf

Although I have pointed out that the connection string connects to the sql server for some reason the web app connects to the newly created localdb database located in APP_DATA.

Am I doing something wrong here?

thanks

  • 1
    Can you provide the relevant sections of your web.config? Are there multiple connection strings in there? Where does the code get the connection string? – DavidG May 31 '14 at 01:09
  • Hi David, I have only the default connection which is connecting to the sqlexpress instance. Here is the web.config, – Computer Scientist May 31 '14 at 12:54

1 Answers1

0

Oddly enough I have just come across this very issue in our codebase. One of my developers was using the 'old' Role provider to check if user was in a particular role. This was causing the app to create the database in App_Data as the config for all that still exists in machine.config. The connection string and provider details are specified in machine.config, so your web.config will only add or remove them. So for example if your machine.config says:

<connectionStrings>
    <add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>

And your web.config has this:

<connectionStrings>
    <add name="MyConnectionString" connectionString="..."/>
</connectionStrings>

Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:

<connectionStrings>
    <clear />
    <add name="MyConnectionString" connectionString="..."/>
</connectionStrings>

But that still leaves the underlying problem that the old role (or profile) provider is being used in code. so you will instead get lots of errors when using it. You need to switch code like this:

if (User.IsInRole("Admin"))
{
    //Do admin stuff
}

To something like this (in my case _userManager is of type UserManager<User> and is injected into my controller constructor at runtime.

var user = _userManager.FindById(User.Identity.GetUserId());

if (_userManager.IsInRole(user.Id, "Admin"))
{
        //Do admin stuff
}
DavidG
  • 113,891
  • 12
  • 217
  • 223