ASP.NET IDENTITY WEB FORMS - asp.net

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

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
}

Related

Umbraco cannot start. A connection string is configured -running correctly in local but error on hosted one

My project is working perfectly on local host but I am trying to host it on iis and following error is showing
Steps which I have tried so far:
1.checked my connection string which is
<connectionStrings>
<remove name="umbracoDbDSN" />
<add name="umbracoDbDSN" connectionString="Server=EIL-IT-360-39\SQLEXPRESS;Database=umbraco_16_6;Integrated Security=true" providerName="System.Data.SqlClient" />
</connectionStrings>
2.Using window authentication to login into my DB.
I have also refrered given sites but could not found desired solution:
Umbraco cannot start. A connection string is configured but the Umbraco cannot connect to the database
Umbraco cannot start after switching to local SQL database
Check which identity your website is running under in IIS:
open IIS
got to Applications Pools in the tree on the left
find your website in the list
go to Advanced Settings
switch the Identity to Network Service rather than ApplicationPoolIdentity
I believe this is the user which runs the website. If this doesn't work, you may want to check that your selected Identity has access to the database.
It looks like a permissions/connection issue. Usually this is because either the IIS server can't see the machine "EIL-IT-360-39" (try using the machine's IP address instead). Or the windows authentication isn't working.
If you're using integrated authentication, the site will be connecting as your application pool identity account. You need to make sure that the application pool account has access to read and write to your database. There is a tutorial here on how to do this: https://msdn.microsoft.com/en-us/library/bsz5788z.aspx
Thank you all for your help..my problem is now solved ..though it solved by adding a new user with some password in my ssms and then adding same in connection string as
<add name="umbracoDbDSN" connectionString="..database=umbraco_16_6;user id=sa;password=*****" providerName="System.Data.SqlClient" />
then,my umbraco password was not accepted at login screen[even though I haven't made any changes in that password]..for which I tried to update hashed password and other options..but none of those worked for me..
Then I have had a backup of my DB..which saved my project..and now I am able to login into my Umbraco site as well as hosted it successfully on my network.
please provide the same user name and password that u created while instating umbraco Db.and yor visualstudio connection string should be like
<connectionStrings>
<remove name="umbracoDbDSN" />
<!--<add name="umbracoDbDSN" connectionString="server=PALPIN-022\SQLEXPRESS;database=Databasename;user id=UmbracoDbname;password='umbracodbpassword'" providerName="System.Data.SqlClient" />-->
<!-- Important: If you're upgrading Umbraco, do not clear the connection string / provider name during your web.config merge. -->
</connectionStrings>
note: password should in "''" double quotes inside single quotes.

Why is my web.api project still looking for an .mdf file?

I have a code first web.api project that was working fine with the automatically created .mdf in the App_Data folder. I decided to move my app to IIS and modify the app pool config to load the user profile, again, no problem. Then I loaded the .mdf in visual studios sql explorer and at that point, the app pool login from the web app started failing. No clue why and couldn't fix it, so I decided to use sql express instead of waste more time on it. So, I installed SQL Express, killed all my migrations, and modified the web.config connection string to:
<connectionStrings>
<add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
When I try to use migrations to update-database or run the api app, I get an exception saying
Cannot attach the file
'C:\Users\Source\MyApp\Main\Solution\MyApp.API\App_Data\MyApp.DataService.DataModel.AppEntities.mdf'
as database 'MyApp.DataService.DataModel.AppEntities'.
Well, that makes sense since I deleted it, but why is my app still trying to connect to the .mdf file? It's gone and I've changed the default connection. I've searched the solution for anything referencing the .mdf file, but nothing shows up. What am I missing?
I finally figured out that I had to name my connection string the same as my DbContext class, so in my case:
<connectionStrings>
<add name="AppEntities" providerName="System.Data.SqlClient" connectionString="Data Source=.;Initial Catalog=MyApp;Integrated Security=SSPI"/>
</connectionStrings>
to match my DbContext derived class
namespace MyApp.DataService.DataModel
{
internal class AppEntities : DbContext

how to find the sql connection string in asp.net

I added a new Sql Server Database to my project with the name: ShDB.mdf. It is located in a App_Data folder.
It contains a Table which has some columns. I could add some numbers to the table and then show them in a gridview on my localhost.
This is what I have in the web.config:
`<configuration>
<connectionStrings>
<add name="ShConnectionStr" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Javad\Desktop\Sharj\App_Data\ShDB.mdf;Integrated Security=True;User Instance=True"/>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ShDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" strict="false" explicit="true" targetFramework="4.0"/>
</system.web>
`
Here is the vb.net code which I used:
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ShConnectionStr").ConnectionString
And it works properly on the localhost test but when I published the website and uploaded the files on a web hosting service to test, I counter an error. How may I fix it? I think I should change my Data source path but I don't know how because its my first experience. Thanks to any help. you can see the error page here: http://www.kaj.somee.com/SL.aspx
Sql Server is either not installed or not running on the web host, or you don't have access to connect to it.
ASP.Net does not by itself know how to use your *.mdf database. It needs a running instance of Sql Server to talk to. It will tell Sql Server to load (attach) your database file, and then send queries to the Sql Server service for execution.
Do you mean you already uploaded the website to a web hosting service, but not the database?
If so, your application may not communicate directly with your database which is actually in your local machine.
You probably need to upload the database to a database hosting service as well (eg.SQL Azure, etc).
You may need to figure out the IP address/Server Name of your database hosting where you have uploaded. These information you can probably get in for the service provider. From there you should be able to connect to the database with the correct configuration.

How to use ASP.NET Configuration on external SQL Server?

In visual studio there is a nice possibility to manage users and memberships for an ASP.NET site. I moved the membership data from SQLEXPRESS to a normal SQL server. The website works fine, however how do i now manage my users/profiles/etc... like I was used to in visual studio? Is it possible to tell vs2010 to 'look' into the new sql server database in stead of the apsnetdb.mdf file?
Thanks,
Erik
This is how I do it:
In your web.config file, under the connectionStrings section, name your connection string, LocalSqlServer, i.e.
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Server=123.123.123.123;Database=dbName;User ID=dbuser;Password=dbPassword;Trusted_Connection=False;" />
</connectionStrings>
Your role and membership providers, if have the connectionStringName set, change them to LocalSqlServer otherwise, the default providers "should" automatically refer to LocalSqlServer anyway.
In VS2010, I then simply go to Project > ASP.NET Configuration which will start the tool to manage users, roles etc.
HTH

after changing asp.net mvc3 connectionstring, asp.net configuration dont work

i have a problem with the asp.net MVC3.
when i create a new project and start the asp.net configuration my browser opens and i can edit e.g. the user.
but when i change the connection string to my external server or to my local sqlexpress server, i get the message after the browser pops up:
The type "MyApp.MvcApplication" could not be loaded.
what do i wrong? my connection string looks like:
<add name="ApplicationServices"
connectionString="Data Source=my-pc\SQLEXPRESS;Initial Catalog=mydb;Integrated Security=SSPI;"
providerName="System.Data.SqlClient" />
Looks like the type hasn't actually been built and packaged as a DLL into the bin folder. Did you try running the project initially before making the change?

Resources