I have two ASP-specific SQL Server databases
1) ASPState - To store session state
2) ASPNETDB - To store Security/Role stuff.
In my web.config, I am specifying the connection string used to identify the location of the APState database:
<sessionState mode="SQLServer" sqlConnectionString="server=(local)\sql2008b;uid=sa;pwd=iainttelling;" timeout="120"/>
Where is the conenction string specified for the ASPNETDB database? I am trying to point it to a db on a remote server.
I have a feeling it is somewhere in IIS orthe Machine Config. I'd like to add it to my WEB.CONFIG Could someone help me to do this?
Define a connection string in connectionStrings section and then override role/membership settings like this:
<system.web>
<roleManager enabled="true" defaultProvider="SqlRoleManager">
<providers>
<add
name="SqlRoleManager"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="DefaultSqlConnection"
applicationName="myApp" />
</providers>
</roleManager>
<membership defaultProvider="SqlMembershipManager" >
<providers>
<clear />
<add
name="SqlMembershipManager"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="DefaultSqlConnection"
applicationName="myApp"
passwordFormat="Hashed" />
</providers>
</membership>
Check this article
Related
I am trying to add AP.Net membership to my database. I have had success with this in the past so I have some idea of what I am doing. My project is a VB.Net website with Framework 4.5.2. I used asp_regsql.exe in framework 4 to add the schema to my db. I created the following web.config entries (per MSDN articles):
<connectionStrings>
<add name="AFKMSConnectionString" providerName="System.Data.SqlClient" connectionString="working as intended" />
<system.web>
<authentication mode="Forms" >
<forms loginUrl="Account/Login.aspx"
name=".ASPXFORMSAUTH" />
</authentication>
<!--<authorization>
<deny users="?" />
</authorization>-->
<compilation debug="true" targetFramework="4.5.2"/>
<httpRuntime targetFramework="4.5.2"/>
<pages>
<namespaces>
<add namespace="System.Web.Optimization"/>
<add namespace="Microsoft.AspNet.Identity"/>
</namespaces>
<controls>
<add assembly="Microsoft.AspNet.Web.Optimization.WebForms" namespace="Microsoft.AspNet.Web.Optimization.WebForms" tagPrefix="webopt"/>
</controls>
</pages>
<membership defaultProvider="SqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add
name="SqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed" />
</providers>
</membership>
<profile enabled ="true" defaultProvider="SqlProvider">
<providers>
<clear />
<add name="SqlProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"
description="SqlProfileProvider for SampleApplication" />
</providers>
</profile>
<roleManager enabled ="true"
defaultProvider ="SqlRoleProvider" >
<providers>
<clear/>
<add name ="SqlRoleProvider"
type="System.Web.Security.SqlRoleProvider"
connectionStringName="AFKMSConnectionString"
applicationName="AFKMS"/>
</providers>
</roleManager>
My connection string works for my current functions but when I try to register a user I get the following error:
Directory lookup for the file "C:\Users\Dan\Source\Repos\AFKMS\AFKMS\App_Data\aspnet-AFKMS-140073d4-6858-4f19-9555-0edfbaadd43a.mdf" failed with the operating system error 2(The system cannot find the file specified.).
CREATE DATABASE failed. Some file names listed could not be created. Check related errors.
This comes from Accounts/Register,
Dim result = manager.Create(user, Password.Text)
Why is it trying to create a database? What does it look like I am missing?
<connectionStrings>
<add name="AFKMSConnectionString" connectionString="data source=.;Initial Catalog=your_db_name;integrated security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
can you provide us with code of how you create users?
you mentioned: Dim result = manager.Create(user, Password.Text)
i think you need to use:
Membership.CreateUser(UserName, Password, Email)
I'm assuming you've created a new ASP.NET Web Application, using the Web Forms templates?
In which case you appear to be attempting to configure the newer ASP.NET Identity system with the original Membership Provider configuration settings.
As Ahmed noted, you mention the call in the register page to manager.Create(user, Password.Text) - this is using the ApplicationUserManager from ASP.NET Identity.
If you look in the App_Start folder of your project, you should find a file named IdentityConfig.vb (assuming you're using VB.NET), which has a method Create that returns the ApplicationUserManager - this in turn calls the ApplicationDbContext class that is created in the Models folder, that will be defined to use a connection called "DefaultConnection" - by default this is set to be a standalone .mdf file in the App_Data folder named after the project.
You should change this to point at your connection string, but be aware that this uses Entity Framework to create the database schema for you - it should just add it to the existing database, but you should ensure you've backed it up before you start in case it drops everything first.
I have created a website for my university that requires membership.
I used the web administration tool from Visual Studio and created Users and Rules.When i test it from Visual Studio it works fine.
The Web Administration Tool automatically creates a SQL database named ASPNETDB.MDF in the App_Data folder that saves membership data.
My question is will the database work when i add the website at the IIS? Do i have to create a sql server database and add a connection?Please answer me.It's really important.
i only want a very basic membership.
You can use db from /App_Data as explained in this SO answer:
ASP.NET: How to deploy App_Data to webhost? - but it depends on your server set up.
When using Membership my preference is to run it from a separate db in either SQL / SQL Express, as then have choice to run IIS site and db on different servers if required. Also better for security / scaling etc.
Once db is created you can use aspnet_regsql.exe to install Membership services into the db. It's usually located here - c:\Windows\Microsoft.NET\Framework\< versionNumber >\aspnet_regsql.exe. Can run as GUI or command line:
http://msdn.microsoft.com/en-us/library/x28wfk74.aspx
When running Membership from a separate db you'll also need to configure a provider in web.config. Something along the lines of this:
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="MyMembershipConnectionString"
connectionString="Data Source=SERVER123\SQLEXPRESS;Initial Catalog=Database123;Persist Security Info=True;User ID=User123;Password=Password123" providerName="System.Data.SqlClient" />
</connectionStrings>
in < System.Web > section:
<roleManager defaultProvider="MySqlRoleProvider" enabled="true"
cacheRolesInCookie="true" cookieName=".ASPROLES" cookieTimeout="30"
cookiePath="/" cookieRequireSSL="false" cookieSlidingExpiration="true" cookieProtection="All">
<providers>
<clear />
<add name="MySqlRoleProvider" type="System.Web.Security.SqlRoleProvider"
connectionStringName="MyMembershipConnectionString" applicationName="MyAppName" />
</providers>
</roleManager>
<membership defaultProvider="MySqlMembershipProvider" userIsOnlineTimeWindow="720">
<providers>
<clear />
<add name="MySqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MyMembershipConnectionString"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
minRequiredPasswordLength="8"
minRequiredNonalphanumericCharacters="0"
passwordStrengthRegularExpression="((?=.*\d)(?=.*[a-z]).{6,20})"
maxInvalidPasswordAttempts="5"
passwordAttemptWindow="30" />
</providers>
</membership>
I've used a similar set up to this a good few times with no problems.
If using db from /App_Data you shouldn't need to do any of the web.config stuff
hello i found an error when i am working with the asp.net web administration tool security tab....and i am using the sqlProvider as the default provider.
There is a problem with your selected data store. This can be caused by an invalid server name or credentials, or by insufficient permission. It can also be caused by the role manager feature not being enabled. Click the button below to be redirected to a page where you can choose a new data store.
The following message may help in diagnosing the problem: An error occurred while attempting to initialize a System.Data.SqlClient.SqlConnection object. The value that was provided for the connection string may be wrong, or it may contain an invalid syntax. Parameter name: connectionString
Do you have a <connectionStrings /> element in your web.config file? You need this to be able to connect to your MembershipProvider and RoleProvider.
Here are the necessary elements you'll need to utilize the SqlMembershipProvider and the SqlRoleProvider.
Notice that there are the following sections:
<connectionStrings />
<membership /> (in the <system.web /> section)
<roleManager /> (in the <system.web /> section)
web.config
<configuration>
<connectionStrings>
<add name="YourConnectionString"
providerName="System.Data.SqlClient"
connectionString="data source=YOURSERVER;
initial catalog=YOURDB;user id=YOURINSTANCELOGIN;password=YOURPASSWORD;"/>
</connectionStrings>
<system.web>
<membership defaultProvider="AspNetSqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="YourConnectionString"
applicationName="YourApplicationName"/>
</providers>
</membership>
<roleManager
enabled="true"
defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear />
<add
connectionStringName="YourConnectionString"
applicationName="YourApplicationName"
name="AspNetSqlRoleProvider"
type="System.Web.Security.SqlRoleProvider" />
</providers>
</roleManager>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
</system.web>
</configuration>
What could be the reason for Roles.CreateRole("admin") creating a new local database inside App_Data, if my default membership provider is set to use a connection to an actual SQL Server?
I have a connection string like this:
<connectionStrings>
<add name="MembershipDbConnection"
connectionString="Server=.;Initial Catalog=aspnetdb;User Id=*;Password=*" />
</connectionStrings>
and a membership provider defined like this:
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add
name="SqlProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="MembershipDbConnection" <-- this is the connection
/>
</providers>
</membership>
The weird thing is, there is an aspnetdb database (populated with tables) in my SQL Server, and if I use Membership.GetAllUsers(), I get the users in that table without problems. Login also works as expected. But Roles.CreateRole creates a new database.
You need to configure the Role manager section if you want to use roles. It is likely you are picking up the inherited RoleManager section that is using a different connection string.
I think you didn't supply "applicationName" attribute in "membership" and "roleManager" tags in Web.Config. Here is an example: -
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="MembershipDbConnection" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<clear />
<add connectionStringName="MembershipDbConnection" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" />
<add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" />
</providers>
</roleManager>
Mansoor Gee's answer provides an excellent template to follow.
Here's a link to documentation of what, and how, should be implemented in the web.config to enable RoleManager:
http://msdn.microsoft.com/en-us/library/vstudio/ms164660(v=vs.100).aspx
Hopefully this will add some context to future readers, as I needed some when I first came across this answer.
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>