This is a continuation of my last question: “Internal Database – ASP.NET Auto Web Site Registration on User Creation”. I am running the following code. This is just a test block of code. It works but my problem is it creates the default SQLEXPRESS ASPNETDB.MDF in the App_Data Directory. This is not what I want.
// Create a MembershipCreateStatus Status for Reporting...
MembershipCreateStatus status = new MembershipCreateStatus();
// Setup SqlMembershipProvider for Initialization...
SqlMembershipProvider sqlProvider = new SqlMembershipProvider();
// Setup a NameValueCollection...
NameValueCollection config = new NameValueCollection();
// Set the Connection Name of the SQL Connection String...
string SQLDBNameString = "My Company Database.Properties.Settings.ConnectionString";
// Update the private connection string field in the base class.
string connectionString = "Data Source=Server;Initial Catalog=’My Company Database';User ID=USERNAME;Password=PASSWORD";
// Username of the account to add...
string username = "User121";
// Generate a dynamic Password....
string password = Membership.GeneratePassword(8, 1);
// Email Address of the User...
string email = "email@email.com";
try
{
string Name = "My Company Database";
config.Add("applicationName", "My Company Database");
config.Add("maxInvalidPasswordAttempts", "5");
config.Add("passwordAttemptWindow", "10");
config.Add("minRequiredPasswordLength", "8");
config.Add("passwordStrengthRegularExpression", "");
config.Add("enablePasswordReset", "True");
config.Add("enablePasswordRetrieval", "False");
config.Add("requiresQuestionAndAnswer", "False");
config.Add("requiresUniqueEmail", "True");
config.Add("passwordFormat", "Hashed");
config.Add("connectionStringName", SQLDBNameString);
sqlProvider.Initialize(Name, config);
ConnectionStringSettings ConnectionStringSettings = ConfigurationManager.ConnectionStrings[SQLDBNameString];
if ((ConnectionStringSettings == null) || (ConnectionStringSettings.ConnectionString.Trim() == String.Empty))
{
throw new Exception("Connection string cannot be blank.");
}
// connectionString = ConnectionStringSettings.ConnectionString;
MembershipUser newUser = Membership.CreateUser(username, password, email, "Whats My Password", password, true, out status);
}
catch (Exception ex)
{
String message = "Error...\r\n\r\n" + ex.ToString();
String caption = "Error";
MessageBoxButtons button = MessageBoxButtons.OK;
MessageBoxIcon icon = MessageBoxIcon.Asterisk;
MessageBox.Show(message, caption, button, icon);
}
MessageBox.Show("Account Creation : " + status.ToString() + "\r\n"
+ "Username is : " + username + "\r\n"
+ "Password is : " + password + "\r\n"
+ "Email : " + email);
This code is running in a C# Application, SQL Front end database and it is supposed to automatically create a user so they can login to a web page once I have added them to my database. This Code is not running in a ASP Website Application. My “config” is not being initalised from what I can see. Any help is much appreaciated!
Finally a solution that works through out my website and C# Internal Database.
A steep learning curve on my part. It is actually much simpler than I thought. Here is the Code that needs to go into the app.config file that needs to be in your C# Project:
Here is the Code to use the default MembershipProvider():
I did try to add a web.config the same as the ASP.NET Web Application but this failed. I did not even think of putting the configuration into app.config untill today. Funny how the brain works when trying to solve a problem.
Password Hashing and Salting is all handled by the default MembershipProvider Class that is built into .NET. No need to do any crazy Encoding Hashing and Salting.