如何为 Aspnetdb 成员数据库创建一个新的 SQL 用户?

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

我使用 asp_regsql.exe 工具而不是现有数据库的默认向导创建了一个成员数据库。 当我使用 aspnetdb 成员数据库的成员 createuser 方法创建新用户帐户时,出现以下错误:

建立与 SQL 服务器的连接时出现与网络相关或特定于实例的错误。服务器未找到或无法访问。验证实例名称是否正确并且 SQL 服务器配置为允许远程连接。 (提供商:SQL 网络接口,错误:26 - 定位 Server/Instance 指定错误)

我正在使用 VS 2013。
我的 aspnetdb 数据库名称是:SecurityTutorials.mdf

我的 web.config 设置:

    <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>



我的 CreatingUserAccounts.aspx.cs 代码是:

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;
        }
    }
} 



您应该将以下行添加到 <system.web></system.web> 块,当然还可以根据需要自定义代码

<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>