简体   繁体   中英

Facebook & Google Login for asp.net website

For my asp.net website I was trying put feature of FB & Google Login. I found tutorial from ASPSNIPPETS . Now in this what I have done is If user click on Facebook Login button then It first authorize the user & get user details in panel. Then I put my code to check whether user already exist in my db or not. If not then it Inserts user & then put login code. My Problem is after executing first line of code it stops executing further so that It is unable to check whether user is logged in or not & so on. I think it may be because of redirect url which stops further codes o execute. How Can I eliminate this problem?

Protected Sub Login(sender As Object, e As EventArgs)
        FaceBookConnect.Authorize("user_photos,email", Request.Url.AbsoluteUri.Split("?"c)(0))

If Not String.IsNullOrEmpty(lblId.Text) Then
            Dim query As String = "Select ID, email From users where ID=@id"
            con.Open()
            Dim cmd As New MySqlCommand(query, con)
            cmd.Parameters.AddWithValue("@id", lblId.Text)
            Dim dr As MySqlDataReader = cmd.ExecuteReader()
            If dr.HasRows Then
                'Do Login Code here
                Try
                    Dim str As String = "select * from users where ID='" + lblId.Text + "';"
                    Dim cmd2 As New MySqlCommand(str, con)
                    Dim da As New MySqlDataAdapter(cmd2)
                    Response.Cookies("User_Type").Value = "users"
                    Response.Cookies("chkusername").Value = lblId.Text
                    Response.Redirect(Request.Url.AbsoluteUri)
                    welcome.Visible = True
                Catch ex As Exception
                    Response.Write(ex)
                End Try
            Else
                Try
                    Dim str1 As String = "INSERT INTO users (ID, DP, displayName) values('" + lblId.Text + "', '" + ProfileImage.ImageUrl.ToString + "', '" + lblName.Text + "')"
                    Dim adapter As New MySqlDataAdapter
                    Dim command As New MySqlCommand
                    dr.Close()
                    command.CommandText = str1
                    command.Connection = con
                    adapter.SelectCommand = command
                    command.ExecuteNonQuery()
                Catch ex As Exception
                    Response.Write(ex)
                End Try


                Dim con2 As New MySqlConnection("connectionstring")
                con2.Open()
                Dim cmd3 As New MySqlCommand("Select ID, email From users where ID=@id", con2)
                cmd3.Parameters.AddWithValue("@id", lblId.Text)
                Dim dr2 As MySqlDataReader = cmd3.ExecuteReader()
                If dr2.HasRows Then
                    'Do Login Code here
                    Try
                        Dim str As String = "select * from users where ID='" + lblId.Text + "';"
                        Dim cmd2 As New MySqlCommand(str, con2)
                        con2.Open()
                        Dim da As New MySqlDataAdapter(cmd3)
                        Response.Cookies("User_Type").Value = "users"
                        Response.Cookies("chkusername").Value = lblId.Text
                        Response.Redirect(Request.Url.AbsoluteUri)
                    Catch ex As Exception
                        Response.Write(ex)
                        con2.Close()
                    End Try
                    con.Close()
                End If

            End If
        End If
        con.Close()
    End Sub

First you can optimise your code :

        If Not String.IsNullOrEmpty(lblId.Text) Then
        Dim str As String = "select * from users where ID=@id"
        con.Open()
        Dim cmd2 As New MySqlCommand(str, con)
        cmd.Parameters.AddWithValue("@id", lblId.Text)
        Dim da As New Common.DataAdapter(cmd2)
        Dim ds As New Data.DataSet()
        da.Fill(ds)
        If ds.Tables(0).Rows.Count > 0 Then
            'Do Login Code here
            Try

                Response.Cookies("User_Type").Value = "users"
                Response.Cookies("chkusername").Value = lblId.Text
                Response.Redirect(Request.Url.AbsoluteUri)
                welcome.Visible = True
            Catch ex As Exception
                Response.Write(ex)
            End Try
        Else
            Try
                Dim str1 As String = "INSERT INTO users (ID, DP, displayName) values(@id, @img,@name)"
                Dim cmd As New MySqlCommand
                cmd = New SqlCommand(req, con)
                cmd.Parameters.Add(New SqlParameter("@id", lblId.Text))
                cmd.Parameters.Add(New SqlParameter("@img", ProfileImage.ImageUrl.ToString))
                cmd.Parameters.Add(New SqlParameter("@name", lblName.Text))
                cmd.ExecuteNonQuery()
            Catch ex As Exception
                Response.Write(ex)
            End Try
            'After insertion you don't have to select  from database just send the parameters to cookies
            Response.Cookies("User_Type").Value = "users"
            Response.Cookies("chkusername").Value = lblId.Text
            Response.Redirect(Request.Url.AbsoluteUri)

        End If
        con.Close()
    End If

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM