添加新用户到使用ASP.. NET成员类

本文关键字:ASP NET 成员类 新用户 用户 添加 | 更新日期: 2023-09-27 17:49:30

我正在使用vs2012 express edition构建webform应用程序。
配置SqlMembership提供程序将用户凭据存储到我的Sqlserver db后,我想创建用户user,但我得到了这个错误:不能声明System.Web.Security.Membership类型的变量。
我花了将近两个小时在谷歌上搜索,仍然没有找到解决方案。这是我的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security.Membership;
//using Microsoft.AspNet.Membership.OpenAuth;
namespace Practice_project.Account
{
public partial class Register : Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //RegisterUser.ContinueDestinationPageUrl = Request.QueryString["ReturnUrl"];
    }
   public void RegisterUser_CreatedUser(object sender, EventArgs e)
    {

        Membership Create_New_User = Membership.CreateUser(UserName, Password);
    }
}
}

添加新用户到使用ASP.. NET成员类

尝试使用另一个超载的CreateUser

  MembershipCreateStatus status;
  MembershipUser newuser = Membership.CreateUser(username, password, email, "none", 
        "none", false, out status);

PasswordQuestionPasswordAnswer设置"none",为IsApproved设置"false/true"

这是asp页面:

<%@ Page Title="Register" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Register.aspx.cs" Inherits="Practice_project.Account.Register" %>
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<hgroup class="title">
    <h1><%: Title %>.</h1>
    <h2>Use the form below to create a new account.</h2>
</hgroup>
<asp:CreateUserWizard runat="server" ID="RegisterUser" ViewStateMode="Disabled" OnCreatedUser="RegisterUser_CreatedUser" BackColor="#E3EAEB" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em">
    <HeaderStyle BackColor="#666666" BorderColor="#E6E2D8" BorderStyle="Solid" BorderWidth="2px" Font-Bold="True" Font-Size="0.9em" ForeColor="White" HorizontalAlign="Center" />
    <LayoutTemplate>
        <asp:PlaceHolder runat="server" ID="wizardStepPlaceholder" />
        <asp:PlaceHolder runat="server" ID="navigationPlaceholder" />
    </LayoutTemplate>
    <ContinueButtonStyle BackColor="White" BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#1C5E55" />
    <CreateUserButtonStyle BackColor="White" BorderColor="#C5BBAF" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana" ForeColor="#1C5E55" />
    <TitleTextStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
    <WizardSteps>
        <asp:CreateUserWizardStep runat="server" ID="RegisterUserWizardStep">
            <ContentTemplate>
                <p class="message-info">
                    Passwords are required to be a minimum of <%: Membership.MinRequiredPasswordLength %> characters in length.
                </p>
                <p class="validation-summary-errors">
                    <asp:Literal runat="server" ID="ErrorMessage" />
                </p>
                <fieldset>
                    <legend>Registration Form</legend>
                    <ol>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="UserName">User name</asp:Label>
                            <asp:TextBox runat="server" ID="UserName" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="UserName"
                                CssClass="field-validation-error" ErrorMessage="The user name field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Email">Email address</asp:Label>
                            <asp:TextBox runat="server" ID="Email" TextMode="Email" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Email"
                                CssClass="field-validation-error" ErrorMessage="The email address field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="Password">Password</asp:Label>
                            <asp:TextBox runat="server" ID="Password" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="Password"
                                CssClass="field-validation-error" ErrorMessage="The password field is required." />
                        </li>
                        <li>
                            <asp:Label runat="server" AssociatedControlID="ConfirmPassword">Confirm password</asp:Label>
                            <asp:TextBox runat="server" ID="ConfirmPassword" TextMode="Password" />
                            <asp:RequiredFieldValidator runat="server" ControlToValidate="ConfirmPassword"
                                 CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The confirm password field is required." />
                            <asp:CompareValidator runat="server" ControlToCompare="Password" ControlToValidate="ConfirmPassword"
                                 CssClass="field-validation-error" Display="Dynamic" ErrorMessage="The password and confirmation password do not match." />
                        </li>
                    </ol>
                    <asp:Button runat="server" CommandName="MoveNext" Text="Register" />
                </fieldset>
            </ContentTemplate>
            <CustomNavigationTemplate />
        </asp:CreateUserWizardStep>

 public void RegisterUser_CreatedUser(object sender, EventArgs e)
    {
        CreateUserProfile(RegisterUser.UserName, RegisterUser.Password);

    }
   private void CreateUserProfile(string username, string password)
   {
       string connect = System.Configuration.ConfigurationManager.ConnectionStrings["_connection"].ToString();
       SqlConnection con = new SqlConnection(connect);
       string insertcommand="Insert into Users"
           +"(Userid,Password)"
           +"Values(@Userid,@Password)";
       SqlCommand cmd = new SqlCommand(insertcommand, con);
       cmd.Parameters.AddWithValue("@Userid", username);
       cmd.Parameters.AddWithValue("@Password", password);
       using (con)
       {
           con.Open();
           cmd.ExecuteNonQuery();
       }


   }
}