locked
Using System.Web.Security.Membership.CreateUser to load users into ASP.NET forms security from a database table of users RRS feed

  • Question

  • User-458170237 posted

    I have been trying for days to figure out how to bulk load users into the ASP.NET security tables.  After googling and experimenting I have the code below.  I've been testing it to get out the bugs but now that no errors display, I don't see any new rows in the users or membership tables.  Can anyone tell me where I'm going wrong?  I am a newbie at .NET coding.

    Here is the code I have so far in the code behind:

    Imports System.Data
    Imports System.Data.Sql
    Imports System.Data.SqlClient
     
    Partial Class Management_BulkInsertUsersToSecurity
      Inherits System.Web.UI.Page
     
    
      Protected Sub Button1_Click(sender As Object, e As EventArgs)
     
        Dim conn As New SqlConnection
        Dim cmd As New SqlCommand
        Try
          conn.ConnectionString = "Data Source=pediatrics02\sea12;Initial Catalog=Quant;User ID=FakeBigUser;Password=FakePW!"
          conn.Open()
          cmd.Connection = conn
          cmd.CommandText = "Select * FROM physician2"
          Dim dr As SqlDataReader = cmd.ExecuteReader()
          If dr.HasRows Then
            While dr.Read()
              'Dim PhysicianID As Guid.NewGUID()
              'Dim PhysicianID As Guid.NewGUID().ToString()
              'Dim PhysicianID As New Guid(dr("PhysicianID").ToString())
              Dim email As String = dr("email").ToString()
              Dim password As String = dr("userpassword").ToString()
              '    Dim applicationID As String = dr('4344BA01-282B-4091-9206-CA6A8A9BCC16').ToString
              Dim status As System.Web.Security.MembershipCreateStatus
              Dim aGUID As String = dr("aGUID").ToString()
              System.Web.Security.Membership.CreateUser(email, password, email, Nothing, Nothing, True, _
                aGUID, status)
            End While
          End If
        Catch ex As Exception
          Response.Write(ex.Message)
        Finally
          conn.Close()
        End Try
     
      End Sub
     
    End Class

    Thanks in advance for your help!

    Here is what I have for a simple form to test:

    <%@ Page Language="VB" AutoEventWireup="false" CodeFile="BulkInsertUsersToSecurity.aspx.vb" Inherits="Management_BulkInsertUsersToSecurity" %>
     
    <!DOCTYPE html>
     
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
       
          <asp:Button ID="Button1" runat="server" Text="Bulk Load New Users (physicians)!" OnClick="Button1_Click" />
          <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:QuantumConnectionString3 %>" SelectCommand="SELECT * FROM [Physician2] WHERE ([Email] IS NOT NULL)"></asp:SqlDataSource>
        </div>
        </form>
    </body>
    </html>

     

    Wednesday, August 27, 2014 9:53 PM

Answers

  • User343834643 posted

    Hi,

    Thanks for posting in ASP.NET forum.

    According to your description, you want to add users in to Membership.

    First, have you debugged code to check whether dr.HasRows is true or not?

    Second, Membership.CreateUser Method (String, String, String, String, String, Boolean, Object, MembershipCreateStatus) will return a status parameter indicating that the user was successfully created or the reason the user creation failed. You could check the result.

    Third, please note while using this method:

    CreateUser adds a new user to the data store and returns a MembershipUser object for the newly created user. If the user creation fails, you can retrieve a MembershipCreateStatus value from the status output parameter that indicates why user creation failed. You can specify a unique identifier for the user, such as a primary key value for a database, by using the providerUserKey parameter.

    The CreateUser method will return null if password is an empty string or null, username is an empty string or null or contains a comma (,), passwordQuestion is not null and contains an empty string, or passwordAnswer is not null and contains an empty string.

    Once a membership user has been created and you have a reference to a MembershipUser object for that user, you can modify the settings for that user with the MembershipUser public methods and by setting the property values of the MembershipUser object and then passing the MembershipUser object to the UpdateUser method.

    If a user already exists in the data source for the application, you can obtain a MembershipUser object for the existing user with the GetUser method.

    Regards,

    George.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2014 5:29 AM

All replies

  • User343834643 posted

    Hi,

    Thanks for posting in ASP.NET forum.

    According to your description, you want to add users in to Membership.

    First, have you debugged code to check whether dr.HasRows is true or not?

    Second, Membership.CreateUser Method (String, String, String, String, String, Boolean, Object, MembershipCreateStatus) will return a status parameter indicating that the user was successfully created or the reason the user creation failed. You could check the result.

    Third, please note while using this method:

    CreateUser adds a new user to the data store and returns a MembershipUser object for the newly created user. If the user creation fails, you can retrieve a MembershipCreateStatus value from the status output parameter that indicates why user creation failed. You can specify a unique identifier for the user, such as a primary key value for a database, by using the providerUserKey parameter.

    The CreateUser method will return null if password is an empty string or null, username is an empty string or null or contains a comma (,), passwordQuestion is not null and contains an empty string, or passwordAnswer is not null and contains an empty string.

    Once a membership user has been created and you have a reference to a MembershipUser object for that user, you can modify the settings for that user with the MembershipUser public methods and by setting the property values of the MembershipUser object and then passing the MembershipUser object to the UpdateUser method.

    If a user already exists in the data source for the application, you can obtain a MembershipUser object for the existing user with the GetUser method.

    Regards,

    George.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 29, 2014 5:29 AM
  • User-458170237 posted

    Thank you George for your detailed answer. This is very useful.

    Friday, August 29, 2014 11:43 PM