Page 1 of 1

How do you setup your application to use remote database...

Posted: Wed Feb 22, 2012 3:39 am
by Trebor29
How do you setup your application to use remote database for membership/roles?

Hello all, im trying to set up membership using a remote database, but I am having a few issues!

I have done this before using the asp.net login control and administration tool with no problems, but this is not so easy.

I am receiving this error when entering the asp.net administration tool (Asp.Net Configuration):

The following message may help in diagnosing the problem: Unrecognized configuration section membership. (C:\Users\ROB\Desktop\PROJECT - Copy\PaymentSystem\web.config line 13)


Which I think is - <add name="AspNetSqlMembershipProvider" - (web.config)


Do I need to include the AspNetSqlMembershipProvider class in my login.cs? Although my code works I am not sure if it is sufficient for what I am trying to do!!? :s

Any help will be gratefully received!

Login.aspx.cs

Code: Select all

public partial class Login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ButtonLogin_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["UserConnectionString"].ConnectionString);
        con.Open();
        string cmdStr = "SELECT COUNT(*) FROM UserTable WHERE EmailAddress= '" + TextBoxLoginEmail.Text + "'";
        SqlCommand checkEmail = new SqlCommand(cmdStr, con);
        int temp = Convert.ToInt32(checkEmail.ExecuteScalar().ToString());
        if (temp == 1)
        {
            string cmdStr2 = "SELECT Password FROM UserTable WHERE EmailAddress= '" + TextBoxLoginEmail.Text + "'";
            SqlCommand checkPword = new SqlCommand(cmdStr2, con);
            string password = checkPword.ExecuteScalar().ToString();
            con.Close();

            if (password == TextBoxLoginPword.Text)
            {
                Session["New"] = TextBoxLoginEmail.Text;
                Response.Redirect("Default.aspx");
            }
            else
            {
                LabelInvalidPword.Visible = true;
                LabelInvalidPword.Text = "Password Not Recognised...!";
            }
        }
        else
        {
            LabelInvalidEmail.Visible = true;
            LabelInvalidEmail.Text = "Email Not Recognised...!";
        }
    }
}
web.config

Code: Select all

<?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>
  <connectionStrings>
    <add name="UserConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    <add name="UserBankDatabaseConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\UserBankDatabase.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    <add name="TheBankDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ROB\Desktop\PROJECT\ExternalBank\App_Data\TheBankDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
    <add name="MerchantBankDBConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\ROB\Desktop\PROJECT\MerchantBank\App_Data\MerchantBankDB.mdf;Integrated Security=True;User Instance=True" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <membership defaultProvider="AspNetSqlMembershipProvider">
    <providers>
      <clear/>
      <add name="AspNetSqlMembershipProvider"
           type="System.Web.Security.SqlMembershipProvider"
           connectionStringName="UserConnectionString"
           passwordFormat="Encrypted"
           enablePasswordRetrieval="true"
           enablePasswordReset="false"
           requiresQuestionAndAnswer="false"
           requiresUniqueEmail="true"
           maxInvalidPasswordAttempts="10"
           minRequiredPasswordLength="5"
           minRequiredNonalphanumericCharacters="0"
           passwordAttemptWindow="30"
           applicationName="/"/>
    </providers>
  </membership>
  <roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
    <providers>
      <clear/>
      <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="UserConnectionString" applicationName="/"/>
    </providers>
  </roleManager>
  <system.web>
    <roleManager enabled="true"/>
    <authentication mode="Forms"/>
    <compilation debug="true" targetFramework="4.0"/>
    <pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
  </system.web>
  <appSettings>
    <add key="BankHost.BankService" value="http://localhost:50743/ExternalBank/BankService.asmx"/>
  </appSettings>
</configuration>

Re: How do you setup your application to use remote database...

Posted: Wed Feb 22, 2012 8:38 am
by Saman
Don't know much on this. But I have found the MS article on this.

Note this part.
Next, you need to add the roleManager and providers elements to register the roleManager provider in the Web.config. Because a default provider is already registered in the machine.config file, you must include a <remove> element prior to the <add> element.

For example:

Code: Select all

<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">

<providers>

<remove name="AspNetSqlRoleProvider" />

<add connectionStringName="SqlProviderConnection" applicationName="/" description="Stores and retrieves roles data from the local Microsoft SQL Server database" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.3600.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" />

</providers>

</roleManager>
I don't see you did this anywhere in the code. Please go through that article and check each step to confirm that you have codes it correctly.

Re: How do you setup your application to use remote database...

Posted: Sun Mar 11, 2012 6:41 pm
by Trebor29
Anyone still interested, this help me a lot:

http://www.csharpaspnetarticles.com/200 ... abase.html