简体   繁体   中英

How to Create A New SQL User For Aspnetdb Membership Db?

I have created a membership database using the asp_regsql.exe tool instead of the default wizard on an existing database. When I create new user account with membership createuser method for aspnetdb membership database, I get below error:

A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)

I am using VS 2013.
my aspnetdb database name is : SecurityTutorials.mdf

my web.config setting :

    <configuration>
      <configSections>
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <connectionStrings>
        <add name="newsdbConnectionString"
             connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\newsdb.mdf;Integrated Security=True"
             providerName="System.Data.SqlClient" />

        <add name="SecurityTutorialsConnectionString"
             connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\SecurityTutorials.mdf;Integrated Security=True"
             providerName="System.Data.SqlClient" />
      </connectionStrings>

      <system.web>
        <compilation debug="true" targetFramework="4.5">
          <assemblies>
            <add assembly="System.Data.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
          </assemblies>
        </compilation>
        <httpRuntime targetFramework="4.5" />
      </system.web>

      <appSettings>
        <add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
      </appSettings>

      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="v11.0" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>
    </configuration>



my CreatingUserAccounts.aspx.cs code is :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.HtmlControls;


public partial class Membership_CreatingUserAccounts : System.Web.UI.Page
{

const string passwordQuestion = "What is your favorite color";
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack) SecurityQuestion.Text = passwordQuestion;

}
protected void CreateAccountButton_Click(object sender, EventArgs e)
{
    MembershipCreateStatus createStatus;
    MembershipUser newUser = System.Web.Security.Membership.CreateUser(Username.Text,
        Password.Text,
        Email.Text,
        SecurityQuestion.Text,
        SecurityAnswer.Text,
        true, out createStatus);
    switch (createStatus)
    {
        case MembershipCreateStatus.Success:
            CreateAccountResults.Text = "The user account was successfully created!";
            break;

        case MembershipCreateStatus.DuplicateUserName:
            CreateAccountResults.Text = "There already exists a user with this username.";
            break;

        case MembershipCreateStatus.DuplicateEmail:
            CreateAccountResults.Text = "There already exists a user with this email address.";
            break;

        case MembershipCreateStatus.InvalidEmail:
            CreateAccountResults.Text = "There email address you provided in invalid.";
            break;

        case MembershipCreateStatus.InvalidAnswer:
            CreateAccountResults.Text = "There security answer was invalid.";
            break;

        case MembershipCreateStatus.InvalidPassword:
            CreateAccountResults.Text = "The password you provided is invalid. It must be seven characters long and have at least one non-alphanumeric character.";
            break;

        default:
            CreateAccountResults.Text = "There was an unknown error; the user account was NOT created.";
            break;
        }
    }
} 



you should add below lines to <system.web></system.web> block,and of course customize the code as you need

<authentication mode="Forms">
      <forms name="/.ASPXAUTH" loginUrl="~/ui/public/ads.aspx" defaultUrl="~/ui/Public/ads.aspx" protection="All" timeout="525600" path="/" requireSSL="false" slidingExpiration="true" cookieless="UseDeviceProfile"/>
</authentication>
<roleManager enabled="true"/>
<membership defaultProvider="AspNetSqlMembershipProvider" userIsOnlineTimeWindow="30" hashAlgorithmType="">
<providers>
        <clear/>
        <add connectionStringName="LocalSqlServer" enablePasswordRetrieval="true" enablePasswordReset="true" requiresQuestionAndAnswer="false" applicationName="/" requiresUniqueEmail="True" passwordFormat="Encrypted" maxInvalidPasswordAttempts="15" minRequiredPasswordLength="4" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</membership>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM