How to connect to Membership DB from an ASP.NET application - asp.net

I am trying to configure my site with ASP.NET Membership to deal with the whole user's login.
For some reason, I can't connect to my DB from the ASP configuration screen. All the TCP/IP are enabled in the SQL server configuration, but for some reason my website can't connect.

The relevant things to check for this issue (that come to my mind) are all in the Web.config file for your application.
You should have a connection to the SQL server set up:
<configuration>
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=ServerName;Initial Catalog=aspnetdb;User Id=sqlUser;Password=sqlPassword" providerName="System.Data.SqlClient"/>
Note: the "connection string" attribute there should point to your SQLExpress instance.
You should have a membership provider pointing to that connection (with some additional setup parameters:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
And (possibly) you should have your authentication mode set to forms:
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880"/>
</authentication>

Related

Using membership functions return user data on development site and not on production site

I've created an asp.net page (not MVC) using the out-of-box template that is created by VS 2010, which includes login controls, using forms authentication. The page provides data review and editing for users in our offices around the Southwest. I am able to access simple enough information from the login database such as names, roles, and email addresses.
The development site and the production site are both on my development computer, if that makes sense. Our actual production site is still under construction, but users are accessing the site through my computer set up as an asp.net server.
At any rate, what I need to do is to create an administrative feature that allows me or other admin users to make changes to the membership data, such as updating email addresses or deleting users.
Using the Membership class, I am able to access the membership data on the development site, but when I deploy it to the (temp) production site it still shows the user data on the development site.
It's clear that the site on the production server is working fine for our users -- i.e. they are able to create their user accounts and to actually log in; but the membership class does not show any of the actual users, only my faux users in the development space.
My web.config file contains these lines:
<connectionStrings>
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient" />
<add name="ConnectionString" connectionString="Data Source=MAINSERVER\SQLEXPRESS;Initial Catalog=XYZ;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
Which seems like it ought to point to the aspnetdb.mdf file in the operating folder.
I've added a gridview just to get familiar with how this all words, and the VB codebehind includes the following lines:
GridView1.DataSource = Membership.GetAllUsers
GridView1.DataBind()
Which works great except it only shows the users in the development site.
I clearly do not understand the login/membership structure.
Can anyone shed some light on this for me?
EDIT: per request from Win----
From the Debug folder:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/>
</providers>
</roleManager>
from the production folder:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider"/>
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider"/>
</providers>
</roleManager>
Near as I can tell, they're identical, and nothing there gives me a clue how to fix it. I hope you can spot it! Thanks!

Login.aspx not using expected SQL Membership Provider database

Any help appreciated!
What is needed so that the Login.aspx page is using the database as shown through the MyDatabaseConnection
I created an ASP.NET Web Forms application
Configured SQL Server Membership Provider (or so I thought) in the main web.config.
Added MyDatabaseConnection:
<connectionStrings>
<add name="DefaultConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-VideoProject-20131115191757;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-VideoProject-20131115191757.mdf"/>
<add name="MyDatabaseConnection"
providerName="System.Data.SqlClient"
connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=videolibrary;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\videolibrary.mdf"/>
</connectionStrings>
MyDatabaseConnection points to the desired database that already has the tables, users and passwords configured (not by me).
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" defaultUrl="~/"/>
</authentication>
...
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="MyDatabaseConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
Created Secure.aspx in Secure folder.
Created web.config in the Secure folder with
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
Using Project\ASP.NET Configuration, I added a user TestAccount.
Added a link in the application that points to Secure.aspx.
When debugging the application, I select the link that points to Secure.aspx. Through login.aspx, I can login as TestAccount and the Secure.aspx page is displayed as expected.
When I use a login user/password from the MyDatabaseConnection database, it fails. That is, login is unsuccessful. It is not looking at this database but I do not know what else is needed for the login.aspx page to use it?
After this, I tried adding AspNetSqlMembershipProvider in the following of the main web.config:
<membership defaultProvider="DefaultMembershipProvider">
<providers>
<add name ="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MyDatabaseConnection"
applicationName="/" />
<add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="MyDatabaseConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/"/>
</providers>
</membership>
but received the following error immediately after trying to login:
Parser Error Message: The entry 'AspNetSqlMembershipProvider' has already been added.
Source Error:
Line 31:
Line 32:
Line 33:
Line 34: type="System.Web.Security.SqlMembershipProvider"
Line 35: connectionStringName="MyDatabaseConnection"
I searched for AspNetSqlMembershipProvider elsewhere (since said already used) within the web.config and did not see it. Note: Unfortunately, I could not use code formatting adequately due to issues using it.
Thanks,
Buck

ASP.NET Membership Mystery

So, I am converting an application from integrated security to forms authentication using the built-in membership provider and having the most bizarre behavior. Initially, I used the default sqlexpress configuration where it creates an aspnetdb.mdf file in your App_Data folder.
However, I wanted to move this to my SQL Server and did so by creating the procedures and tables in an SQL Server database and pointed my provider at that database. And yet, when I would run the Web Site Administration Tool, it would not look at the SQL Server.
It keeps creating an aspnetdb.mdf file and using that, even though my application appeared to be properly hitting the SQL database. Finally, I turned off the SQLEXPRESS service, and now is where the really odd bit happens.
Login works fine - Membership.Provider.ValidateUser(LoginUser.UserName, LoginUser.Password) returns true. However, when an IsInRole call is made, it times out failing to connect to the database. Clearly, it is trying to connect to the SQLEXPRESS database. I have restarted IIS, restarted the database service to no avail.
Is this not the right place to tell the membership service what database to hit:
<connectionStrings>
<add name="MyConnection" connectionString="user id=myuser;password=mypassword;data source=SERVER\INSTANCE;initial catalog=mycatalog;"/>
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms name="formsauth" protection="None" path="/" loginUrl="~/Account/Login.aspx" cookieless="UseCookies"/>
</authentication>
<roleManager enabled="true" />
<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="MyConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="/MyApp"
requiresUniqueEmail="false"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""
/>
</providers>
First and foremost, the reason why the aspnetdb.mdf is being created is because you're using the ASP.NET Configuration Tool. It only works for the SQL Express instance. It can't be used on a fully fledged SQL Server. For that, you'll essentially need to re-create the administration panel within your own application.
If you wish to use Roles under the local SQL Server, you'll need to configure your web.config to point to your SQL Server for the role provider as well. By default it points to the aspnetdb.mdf file that the Membership provider also points to.
For example:
<system.web>
<roleManager defaultProvider="SqlRoleProvider" enabled="true">
<providers>
<clear />
<add name="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="MyConnection"/> <!--- Point this to your sql server -->
</providers>
</roleManager>
</system.web>
For you it would look something like this:
<configuration>
<connectionStrings>
<add name="MyConnection" connectionString="user id=myuser;password=mypassword;data source=SERVER\INSTANCE;initial catalog=mycatalog;"/>
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms name="formsauth" protection="None" path="/" loginUrl="~/Account/Login.aspx" cookieless="UseCookies"/>
</authentication>
<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="MyConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
applicationName="/MyApp"
requiresUniqueEmail="false"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
passwordAttemptWindow="10"
passwordStrengthRegularExpression=""/>
</providers>
</membership>
<roleManager defaultProvider="SqlRoleProvider" enabled="true">
<providers>
<clear />
<add name="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="MyConnection"/> <!--- Point this to your sql server -->
</providers>
</roleManager>
</system.web>
</configuration>
See here for more details: Membership and Role Provider

how to change ASP.NET Configuration tool connection string

how can I change ASP.NET Configuration tool-s connection string name?
(Which connection string will ASP.NET Configuration tool will use)
I'm learning ASP.NET and everywhere and in book that I'm reading now theres connection string named: LocalSqlServer.
I want to use my local sql server database instead of sql express to store Roles, Membership and other data.
I have used aspnet_regsql.exe to create needed data structures in my database. after that I changed my web.config to look like:
<connectionStrings>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Server=(LOCAL); Database=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
but when I run ASP.NET Configuration tool it says that:
"The connection name 'ApplicationServices' was not found in the applications configuration or the connection string is empty."
ASP.NET Configuration tool uses connection string named: ApplicationServices not LocalSqlServer.
cause of that I have to modify web.config to:
<connectionStrings>
<add name="ApplicationServices" connectionString="Server=(LOCAL); Database=MyDatabase;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
and everything works fine.
I wish to know why the hell my web site uses connection string named: ApplicationServices and all books and online documentations uses LocalSqlServer? and how to change it to LocalSqlServer?
I have:
Windows 7
Sql Server 2008 R2
Visual Studio 2010 Premium
Project type is website
accidentally I have found my question answer when looking to web.config file.
if you override default machine.config configuration settings in web.config file you can change ASP.NET Configuration tool-s connection string name.
I got my web.config file from book-s code archive and it was the problem.
in web.config u can override which connection string name will be used for: membership, profile and roleManager.
to override membership use:
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="LocalSqlServer"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"/>
</providers>
</membership>
where connectionStringName is the name of connection string which will be used for storing membership data.
others are:
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="LocalSqlServer"
applicationName="/"/>
</providers>
</profile>
and
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="LocalSqlServer" applicationName="/"
name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider"
type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>

Changing ASP.NET Login Database

In my test enviroment I created a Login and used the ASP.NET Configuration in Visual Studio. It worked perfectly.
But now after testing I imported an existing database to my sql-server and this database includes existing asp.net login tables(same structure). In my web-application I want to use these imported tables instead of those in my testing database. I already checked the web.config as well as the aspnetreg tool (don't know the exact name :p)
My question: How can I change the database used by my ASP.NET login?
you have to overwrite the default application services connection string to have it use your existing deployed DB
in the web.config connection string section change the default connection string
<add name="ApplicationServices" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
to be whatever your connection string is
or if you want to use a different connection string you can change the name of the connection string used in the membership provider settings also in web.config
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>
</providers>
</membership>
Change the name of the connectionStringName to something else
EDIT Role provider code
<roleManager enabled="true">
<providers>
<clear/>
<add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</roleManager>
Again you will want to change connectionStringName to your connection string.

Resources