I have a web application which uses membership and profiles. I successfully used the WebProfileBuilder extension, so my profile class is correctly generated, and is working nicely.
However, after a new request of my client, I need now to move that profile management part into another assembly (so I'd be able to get profile information in a windows service running on the same machine).
What I made is created the new assembly, moved my generated profile file, and tried to use it from the other assembly, but without any success. I always get a SettingsPropertyNotFoundException. My thought is that the profile system doesn't know where to find its connection information, so I tried to add the connectionstring and provider in the app.config of this assembly, but this doesn't seem to work.
What am I missing ? Is it possible to do ?
Thanks in advance!
I've got a nasty suspicion that your APP.Config file won't be picked by the web application; did you keep the settings in your Web.Config file ?
I've only found app.config to work on such an assemply when using NUnit or similar.
Ok I found what's wrong... Thanks to this blog post:
http://fredrik.nsquared2.com/viewpost.aspx?postid=244&showfeedback=true
The only thing I needed to do is add applicationName="/" in my provider configuration, in the app.config. (which is the application name, can be found inside the aspnet_Applications table in the DB.
<configuration>
<connectionStrings>
<add
name="MyConnectionString"
connectionString="Data Source=...;Initial Catalog=...;User ID=...;Password=..."
providerName="System.Data.SqlClient" />
</connectionStrings>
<system.web>
<profile
enabled="true"
defaultProvider="Sql2008ProfileProvider">
<properties>
<add name="UserLevel" type="integer"/>
<add name="value1" type="string" />
<add name="value2" type="string" />
<add name="value3" type="string" />
<add name="value4" type="string"/>
</properties>
<providers>
<clear/>
<add
name="Sql2008ProfileProvider"
type="System.Web.Profile.SqlProfileProvider"
connectionStringName="MyConnectionString"
applicationName="/"
/>
</providers>
</profile>
</system.web>
</configuration>
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 am migrating a ASP.NET webforms website to a new server. On the old server I had my database in the App_Data folder in the root of the website. On my new server, I have the database in a different folder outside the root and that all works fine.
But something strange happens. When I go to the website and surf around, suddenly a App_Data folder is being created in the root folder of my site with a database in it, called ASPNETDB.MDF. I can delete it but after a while it appears again.
I did some testing and trying, and I found out that the DB is being created when I run this code:
TestLabel.Text = Profile.MyProfileParameter.ToString();
So I think it must be the Profile being called that causes it.
In my web.config I have the following: (a bit reduced, i have more profile parameters)
<profile enabled="true">
<properties>
<add name="MyProfileParameter" type="Int32" defaultValue="30"/>
</properties>
</profile>
A bit higher up in my web.config i have the connectionstring that points to the database outside my root folder:
<connectionStrings>
<add name="ConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TheNameOfMyDatase;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
Does anybody have any idea why a call to profile would generate a database?
It's not a real show stopper, but I'd like to know why this is happening.
Some extra info:
My old server has Windows Server 2003
My new server has Windows Server 2008 R2
My application is running in ASP.NET 3.5
SOLUTION:
Apparently there still a lot for me to learn about web.config.
Apart from the web.config there is also a machine.config from which the web.config inherits.
In this machine config in the section <profile> there is this setting:
<profile>
<providers>
<add name="AspNetSqlProfileProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</profile>
This setting can be overridden in the web.config
First (I do not think this is really necessary ) I remove the localSqlServer from the connection strings by adding a clear command to my <connectionStrings> :
<connectionStrings>
<!-- clear command removes localSqlServer from machine.config -->
<clear />
<add name="ConnectionString" providerName="System.Data.SqlClient"
connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=TheNameOfMyDatase;Integrated Security=True;MultipleActiveResultSets=True" />
</connectionStrings>
If I run my site now you get an error saying the connection name 'LocalSqlServer' was not found.
So I added a new provider to the <profile> section and point it to my ConnectionString
<profile enabled="true">
<providers>
<!-- clear command removes localSqlServer from machine.config -->
<clear/>
<add name="AspNetSqlProfileProvider" connectionStringName="ConnectionString" applicationName="/" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
Now it all works fine, no more database appearing out of the blue.
Thank you ijaz for giving directions, the bonus is yours!
AspNetdb is a database used by Asp.Net Membership provider or other application services like "profiles,Roles/Rights etc" The generated .mdf file is a database (SQL 2005 express).
It stores the information you store in your profile or membership or roles, when you use the standard sql providers in ASP.NET. App_Data is the default location of this db,but off-course it can be changed. try to find and update the section of web.config file as,
<membership>
<providers>
</providers>
</membership>for details,please have a look at the following post.
http://forums.asp.net/t/1517995.aspx
I'm attempting to make the login requirements for Password less strict. I set all of my attributes in the web.config file, but for some reason, it doesn't like my Type attribute. I've looked at all of the other posts concerning web.config Membership on StackOverflow, as well as other sites, and they all seem to have the exact line that I do for Type. I can't seem to find much about this error in my searches, either. I just can't figure out why I am getting this error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Attribute not recognized 'writeExceptionsToEventLog'
Source Error:
Line 16: <clear />
Line 17: <add name="AspNetSqlMembershipProvider"
Line 18: type="System.Web.Security.SqlMembershipProvider"
Line 19: connectionStringName="CafeWorksConnectionString"
Line 20: requiresQuestionAndAnswer="false"
Here is my web.config file (slightly modified to remove sensitive info)
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<appSettings>
<add key="validationSettings:UnobtrusiveValidationMode" value="None"/>
</appSettings>
<system.web>
<authentication mode="Forms" />
<compilation debug="true" strict="false" explicit="true" targetFramework="4.5"/>
<httpRuntime targetFramework="4.5"/>
<membership defaultProvider="AspNetSqlMembershipProvider" userIsOnlineTimeWindow="15">
<providers>
<clear />
<add name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider"
connectionStringName="CafeWorksConnectionString"
requiresQuestionAndAnswer="false"
enablePasswordRetrieval="false" enablePasswordReset="true"
requiresUniqueEmail="false" passwordFormat="Hashed"
minRequiredNonalphanumericCharacters="0" writeExceptionsToEventLog="false"
minRequiredPasswordLength="4" passwordStrengthRegularExpression=""
passwordAttemptWindow="10" maxInvalidPasswordAttempts="8"/>
</providers>
</membership>
</system.web>
<system.net>
<mailSettings>
<smtp from="Name#EmailHost.com">
<network host="EmailHost" password="" userName="" />
</smtp>
</mailSettings>
</system.net>
<connectionStrings>
<add name="CafeWorksConnectionString" connectionString="Data Source=DB;Initial Catalog=DBCatalog;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
</configuration>
If anyone knows how to correct this error I'd appreciate the help. I have absolutely no idea where "writeExceptionsToEventLog" is taking place and why it is causing my error.
Thank you!
According to your last comment, you are using legacy Membership Provider. It uses store procedures and dated.
Please use ASP.NET Universal Providers which uses Entity Framework.
How to replace with New Membership Provider
I assume you are implementing a new project. The reason is new Universal Provider creates table without prefix aspnet_ which is good. However, it'll break the relationship with custom tables if you are working on an existing project.
Follow the Scott Hanselman's article and work on a new test project first.
Basically, you just need to replace name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider".
Since you replace the name, you also need to up the defaultProvider to - defaultProvider="DefaultMembershipProvider"
The rest are the same. New Universal Provider uses Entity Framework Code First, so it'll create required tables for you automatically if they haven't been created yet. Note, it doesn't use store procedure.
The beauty is that you do not need to run aspnet_regsql.exe to create schema and store procedures.
I am new in the web development world and I would like to create a variable in the web.config file so that I can use it in the .NET portion of the web.api
I found the following tutorials on how to do that :
Setting up connection string in ASP.NET to SQL SERVER
And
http://www.connectionstrings.com/Articles/Show/store-connection-string-in-web-config
I have the following question , I don t have a database to connect the string to(I will only use it in the web config so that I can easily change the string without having to go through code . so assuming that I am using it in the following way :
<add name="ConnStringDb1" connectionString="Data Source=localhost;Initial Catalog=YourDataBaseName;Integrated Security=True;" providerName="System.Data.SqlClient" />
What should I have in the connectionString and the providerName ?
If I understand what you want to do, it sounds like you don't want to use a connection string at all. Instead, use the Application Settings sections of your web.config file. For example
<configuration>
<system.web> ... </system.web>
<appSettings>
<add key="MyAppSetting" value="A test value." />
</appSettings>
</configuration>
This can then be used in your code by getting the value of
System.Configuration.ConfigurationManager.AppSettings["MyAppSetting"]
(C#) or
System.Configuration.ConfigurationManager.AppSettings("MyAppSetting")
(VB)
See MSDN for more information, or just search online for "asp.net AppSettings".
If you don't have a database to connect to (which is what I understood from your question), then you don't even need to have the <connectionStrings> section in your Web.config. That section is only needed if you are going to connect to a database.
If you do use a database, then the connectionString varies depending on several factors such as type of authentication, database product (MS SQL Server, MySQL), type of driver (ODBC, .NET), etc.
The "Provider Name" will depend on the database product that you are using. For example for SQL Server is "System.Data.SqlClient"
You can look at this site for a comprehensive list of database products and connection strings appropriate for each product for different authentication types, drivers used, etc.
For ASP.NET 4.5 Application I'm using appSettings for email configuration. I'm also using connectionStrings
appSettings needs to be included before connectionStrings not before configSections
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<appSettings>
<add key="ContactEmail0" value="service#davincispainting.com" />
<add key="ContactEmail1" value="estimate#davincispainting.com" />
</appSettings>
<connectionStrings>
<!--<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-DAV3-20150302043828.mdf;Initial Catalog=aspnet-DAV3-20150302043828;Integrated Security=True" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=*****;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=localhost;Initial Catalog=Davincis3;User ID=*****;Password=*****;" providerName="System.Data.SqlClient" />-->
<!--<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLSERVEREXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />-->
<add name="connectionString" connectionString="data source=DELLLAPTOP-PC\SQLEXPRESS;Initial Catalog=Davincis3;User ID=sa;Password=*****;" providerName="System.Data.SqlClient" />
</connectionStrings>
...
I have the entries below in my Web.config and I am using .NET 2.0 and C# for coding.
<add key="userName" value="s752549"/>
<add key="userPassword" value="Delhi#007"/>
Now I want this to be encrypted so that nobody can see it, and also these passwords may change frequently (every fifteen days).
Just wanted to add to this, the marked answer was 99% complete, but it didn't provide how to specify the location of the web config. Rather than root around the internet, thought I'd just post the complete command. As such, here is the command I executed
C:\Windows\Microsoft.NET\Framework64\v4.0.30319>aspnet_regiis -pef "secureAppSettings" "C:\MyLocalPublishDirectory\MyApp" -prov DataProtectionConfigurationProvider
You could put the username and password into a separate section and encrypt this section only. For example:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<appSettings>
<add key="Host" value="www.foo.com" />
<add key="Token" value="qwerqwre" />
<add key="AccountId" value="123" />
<add key="DepartmentId" value="456" />
<add key="SessionEmail" value="foo#foo.com" />
<add key="DefaultFolder" value="789" />
</appSettings>
<secureAppSettings>
<add key="userName" value="s752549"/>
<add key="userPassword" value="Delhi#007"/>
</secureAppSettings>
</configuration>
and then use aspnet_regiis
For Ex:
aspnet_regiis -pef secureAppSettings . -prov DataProtectionConfigurationProvider
You can Protect / Unprotect entire config sections in .NET.
For more info see http://www.codeproject.com/Articles/38188/Encrypt-Your-Web-config-Please.aspx
you could use aspnet_regiis, see http://msdn.microsoft.com/en-us/library/zhhddkxy(v=VS.80).aspx