Using multiple accounts for single login in ASP.Net

RichardSa
 
on Dec 01, 2022 01:45 AM
380 Views

Hello everyone,
I REALLY NEED YOUR ASSISTANCE/HELP ON THIS I have a very fascinating design which I want to achieve in my project. It may seem complex though but it will be great if I achieve it and I hope it will not be too misunderstanding; I hope you understand what I mean, and I will be totally grateful if I get help. Thank you. If I have registered Admin (Admin A and Admin B) on my website application. I want a situation where Admin can invite users to be on his team. But what if the user is already registered to another Admin? Example: There are two Admin accounts currently registered (Admin A and Admin B), with emails (admin1@mail.com and adminB@mail.com). Then I have user A, with email (UserA@mail.com). When Admin A invites user A (UserA@mail.com) to join his team; if UserA@mail.com does not exists, then it should send an invite button for User A to register. But if UserA@mail.com exists then it should send a button for User A to login. HERE IS WHERE IT BECOMES TRICKY: If User A exists it simply means that User A is already registered under an Account; and has a password. Now, when Admin A sends User A an invite and User A is already registered; at that point Admin A must have re-inserted User A’s email and the same password User A used when registering, but under a different Name (which means that Admin A re-inserted User A’s existing login details but under Admin A’s name). Now User A will be registered under two admin accounts, so that after login and redirecting to dashboard, an option to switch accounts will be displayed on the dashboard. The user can switch account which he or she wants to work on. This is a sample image where user switches account.
At the same time, user A can register his own account as an admin with the same email (UserA@mail.com) but different name, because user A has existing account as a user to Admin A and Admin B, and can either work for Admin A, Admin B or for self. The Table Schema will look like below, but I don’t know if it can get mixed up upon login. Maybe when someone tries to login it could cause irregularities in the table and maybe give errors

Id email Name Role Pass_word
1 ADMINa@mail.com Corel Children School Admin adminApass
2 adminb@mail.com Unique Schools Admin passforAdminB
3 UserA@mail.com Unique Schools User @UserApass
4 UserA@mail.com Mikey Furniture Admin @UserApass
5 buser@mail.com Unique Schools User userb
6 UserA@mail.com Corel Children School User @UserApass
Download FREE API for Word, Excel and PDF in ASP.Net: Download
Shivam.M
 
on Dec 02, 2022 06:49 AM

Hi RichardSa,

Please refer below sample.

HTML

<table>
    <tr>
        <td>Username: </td>
        <td><asp:TextBox ID="txtUsername" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Password: </td>
        <td><asp:TextBox ID="txtPassword" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Email: </td>
        <td><asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Admin: </td>
        <td><asp:TextBox ID="txtAdmin" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td></td>
        <td><asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="OnSubmit" /></td>
    </tr>
</table>

Namespaces

C#

using System.Data;
using System.Configuration;
using System.Data.SqlClient;

VB.Net

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Code

C#

protected void OnSubmit(object sender, EventArgs e)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM userTable WHERE Email=@Email OR Username=@Username", con))
        {
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
            cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
            con.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + "Record already exist." + "')", true);
                con.Close();
            }
            else
            {
                con.Close();
                con.Open();
                string conString1 = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
                using (SqlConnection con1 = new SqlConnection(conString1))
                {
                    using (SqlCommand cmd1 = new SqlCommand("Insert INTO userTable VALUES(@Username, @Password, @Email, @Admin)", con))
                    {
                        cmd1.Parameters.AddWithValue("@Username", txtUsername.Text.Trim());
                        cmd1.Parameters.AddWithValue("@Email", txtEmail.Text.Trim());
                        cmd1.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
                        cmd1.Parameters.AddWithValue("@Admin", txtAdmin.Text.Trim());
                        cmd1.ExecuteNonQuery();
                        con1.Close();
                        ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('" + "Record Inserted Sucessfully." + "')", true);
                    }
                }
            }
        }
    }
}

VB.Net

Protected Sub OnSubmit(ByVal sender As Object, ByVal e As EventArgs)
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand("SELECT * FROM userTable WHERE Email=@Email OR Username=@Username", con)
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
            cmd.Parameters.AddWithValue("@Username", txtUsername.Text.Trim())
            con.Open()
            Dim dr As SqlDataReader = cmd.ExecuteReader()

            If dr.HasRows Then
                ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & "Record already exist." & "')", True)
                con.Close()
            Else
                con.Close()
                con.Open()
                Dim conString1 As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
                Using con1 As SqlConnection = New SqlConnection(conString1)
                    Using cmd1 As SqlCommand = New SqlCommand("Insert INTO userTable VALUES(@Username, @Password, @Email, @Admin)", con)
                        cmd1.Parameters.AddWithValue("@Username", txtUsername.Text.Trim())
                        cmd1.Parameters.AddWithValue("@Email", txtEmail.Text.Trim())
                        cmd1.Parameters.AddWithValue("@Password", txtPassword.Text.Trim())
                        cmd1.Parameters.AddWithValue("@Admin", txtAdmin.Text.Trim())
                        cmd1.ExecuteNonQuery()
                        con1.Close()
                        ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('" & "Record Inserted Sucessfully." & "')", True)
                    End Using
                End Using
            End If
        End Using
    End Using
End Sub