如何以编程方式创建会员用户? [英] How to create membership users programmatically?

查看:109
本文介绍了如何以编程方式创建会员用户?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在以编程方式创建成员数据库的用户时,我一直在寻找解决方案,但到目前为止,对于我需要的解决方案没有任何帮助.

我希望这里有人可以帮助我,或者至少可以指导我正确的方向.

我将用几句话来尝试解释我在这里要完成的工作.我的第一步是在SQL Server中创建两个成员数据库,以适应查询统计数据时用户的分离. "我认为,从A组与B组中提取数据时,这样会更容易"

至于第二步,我想使用CreateUserWizard控件以编程方式创建用户,并能够设置要将用户添加到哪个成员资格数据库.

为了简短起见,我相信我的web.confg文件下有正确的代码可以表示两个成员数据库.

这样的步骤应该很容易,对吗? 1.调用控件的事件处理程序2.设置到事件处理程序内部的成员资格数据库的连接3.使用Membership.CreateUser方法添加新用户

但是,在"CreateUserWizard1_CreatingUser"处理程序下添加用户时,在"Membership.CreateUser"方法下出现了一条弯曲的蓝色线条. "在VB.Net "

如果我不清楚我在这里要问的是什么,或者您需要更多信息,请随时问我.


谢谢

Hello,

I have been searching and searching for a solution when it comes to programmatically creating users to the membership database, but nothing so far has worked for what I need.

I hope someone here can help me out or at least guide me in the right direction.

In a few sentences I will try to explain what I am trying to accomplish here. My first step was to create two membership databases in my SQL server to accommodate the separation of users when querying statistical data. "I thought it would be easier this way when pulling data from group A vs. group B"

As far as my second step I would like to programmatically create users using the CreateUserWizard control and be able to set which membership database to add users to.

To keep it short I am confident that I have the right code under my web.confg file to represent the two membership databases.

The steps should be easy like this right? 1. Call the control''s event handler 2. Set connection to the membership database inside the event handler 3. Use the Membership.CreateUser method to add new users

However I get the blue squiggly line under the "Membership.CreateUser" method when adding users under the "CreateUserWizard1_CreatingUser" handler. "In VB.Net"

If I am not clear on what I am asking here, or you need more information, please, please be free to ask me.


Thanks

推荐答案

在您的webconfig文件中添加以下代码

Add following code in your webconfig file

<authentication mode="Forms">
            <forms loginUrl="default.aspx">
            </forms>
        </authentication>
        <membership defaultProvider="defaultMembershipProvider">
            <providers>
                <add name="defaultMembershipProvider" connectionStringName="ConnectionStringNmae" applicationName="ss" type="System.Web.Security.SqlMembershipProvider" requiresQuestionAndAnswer="true" passwordFormat="Clear" enablePasswordRetrieval="true" enablePasswordReset="true" minRequiredNonalphanumericCharacters="0" maxInvalidPasswordAttempts="999" passwordAttemptWindow="60" minRequiredPasswordLength="7"/>
            </providers>
        </membership>
        <roleManager enabled="true">
            <providers>
                <clear/>
                <add name="AspNetSqlRoleProvider" connectionStringName="ConnectionStringNmae" applicationName="/TESTProject" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            </providers>
        </roleManager>



然后

执行regsql.exe并定义数据库.Membership表自动创建.

在您的用户创建页面中,提交您的详细信息时添加以下代码.




Then

execute your regsql.exe and define your db .The Membership table automatically create.

In your user creation page , add following code when submit your details.


if (Page.IsValid)
      {

              MembershipCreateStatus UserCreateStatus;

              //MembershipUser uu = Membership.CreateUser(txtUserName.Text, txtPassword.Text, txtEmailid.Text, "1", "1", true, out UserCreateStatus);

              MembershipUser uu = Membership.CreateUser(txtUserName.Text  , txtPassword.Text, txtUserName.Text  + "@test.com", "1", "1", true, out UserCreateStatus);

              switch (UserCreateStatus)
              {
                  case MembershipCreateStatus.Success:

                      Object[] _Obj = new Object[] { 0, 1, 2, 3, 4 };
                      if (Roles.RoleExists(ddUserGroup.SelectedItem.ToString() + "_" + Session["CompanyID"].ToString()))
                      {
                          Roles.AddUserToRole(txtUserName.Text , ddUserGroup.SelectedItem.ToString() );

                          try
                          {


                              txtName.Text = "";
                              txtPassword.Text = "";

                              ddUserGroup.SelectedValue = "0";
                              lblError.Text = "";
                              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('User created successfully!');</script>", false);


                          }
                          catch
                          {
                              Membership.DeleteUser(txtUserName.Text  );

                              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "<script>alert('User unable to create!');</script>", false);


                          }



                      }
                      else
                      {
                          lblError.Text = "Invalid Type Name.";
                      }
                      break;

                  case MembershipCreateStatus.DuplicateUserName:
                      lblError.Text = "";
                      lblError.Text = "Username already exists. Please enter a different user name.";
                      break;
                  case MembershipCreateStatus.DuplicateEmail:
                      lblError.Text = "";
                      lblError.Text = "A username for that e-mail address already exists. Please enter a different e-mail address.";
                      break;
                  case MembershipCreateStatus.InvalidPassword:
                      lblError.Text = "";
                      lblError.Text = "The password provided is invalid. Please enter a valid password value. The Password Length Min 7 character";
                      break;
                  case MembershipCreateStatus.InvalidEmail:
                      lblError.Text = "";
                      lblError.Text = "The e-mail address provided is invalid. Please check the value and try again.";
                      break;
                  case MembershipCreateStatus.InvalidUserName:
                      lblError.Text = "";
                      lblError.Text = "The user name provided is invalid. Please check the value and try again.";
                      break;
                  default:
                      lblError.Text = "";
                      lblError.Text = "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";
                      break;

              }

          }


这篇关于如何以编程方式创建会员用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆