简体   繁体   English

Facebook和Google登录asp.net网站

[英]Facebook & Google Login for asp.net website

For my asp.net website I was trying put feature of FB & Google Login. 对于我的asp.net网站,我尝试了FB和Google Login的功能。 I found tutorial from ASPSNIPPETS . 我从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. 现在,我所做的就是如果用户点击Facebook登录按钮,那么它首先授权用户并在面板中获取用户详细信息。 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. 我认为这可能是因为重定向url会停止执行更多代码。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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