Imports System.IO Imports System.Web.Configuration Imports System.Web.UI.WebControls Imports System.Net.Http Imports System.Web.Script.Serialization Imports Newtonsoft.Json Imports System.Web Imports System.Web.UI Imports System.Net Partial Class Userlogin Inherits System.Web.UI.Page Protected googleplus_client_id As String = "36945480091-urrukj1mot5rnv0n7n7aercc90d74s76.apps.googleusercontent.com" ' Replace this with your Client ID Protected googleplus_client_sceret As String = "Zf4cxfi0gd7i9apCVnDS-sb5" ' Replace this with your Client Secret Protected googleplus_redirect_url As String = "http://localhost:31133/studentOnline/Login.aspx" ' Replace this with your Redirect URL; Your Redirect URL from your developer.google application should match this URL. Protected Parameters As String Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load GoogleLogin() 'Session("Username") = TextBoxUser.Text 'Session("Password") = TextBoxPassword.Text End Sub Protected Sub Google_Click(sender As Object, e As EventArgs) Handles ButtonLogin.Click Dim Googleurl = "https://accounts.google.com/o/oauth2/auth?response_type=code&redirect_uri=" & googleplus_redirect_url & "&scope=https://www.googleapis.com/auth/userinfo.email%20https://www.googleapis.com/auth/userinfo.profile&client_id=" & googleplus_client_id Session("loginWith") = "google" Response.Redirect(Googleurl) End Sub Private Sub GoogleLogin() If Session.Contents.Count > 0 Then If Session("loginWith") IsNot Nothing Then If Session("loginWith").ToString() = "google" Then Try Dim url = Request.Url.Query If url <> "" Then Dim queryString As String = url.ToString() Dim delimiterChars As Char() = {"="c} Dim words As String() = queryString.Split(delimiterChars) Dim code As String = words(1) If code IsNot Nothing Then 'get the access token Dim webRequest__1 As HttpWebRequest = DirectCast(WebRequest.Create("https://accounts.google.com/o/oauth2/token"), HttpWebRequest) webRequest__1.Method = "POST" Parameters = "code=" & code & "&client_id=" & googleplus_client_id & "&client_secret=" & googleplus_client_sceret & "&redirect_uri=" & googleplus_redirect_url & "&grant_type=authorization_code" Dim byteArray As Byte() = Encoding.UTF8.GetBytes(Parameters) webRequest__1.ContentType = "application/x-www-form-urlencoded" webRequest__1.ContentLength = byteArray.Length Dim postStream As Stream = webRequest__1.GetRequestStream() ' Add the post data to the web request postStream.Write(byteArray, 0, byteArray.Length) postStream.Close() Dim response__2 As WebResponse = webRequest__1.GetResponse() postStream = response__2.GetResponseStream() Dim reader As New StreamReader(postStream) Dim responseFromServer As String = reader.ReadToEnd() Dim serStatus As GooglePlusAccessToken = JsonConvert.DeserializeObject(Of GooglePlusAccessToken)(responseFromServer) If serStatus IsNot Nothing Then Dim accessToken As String = String.Empty accessToken = serStatus.access_token ' This is where you want to add the code if login is successful. If Not String.IsNullOrEmpty(accessToken) Then getgoogleplususerdataSer(accessToken) End If End If End If End If Catch ex As Exception 'Throw New Exception(ex.Message, ex) Response.Redirect("Login.aspx") End Try End If End If End If End Sub Public Class GooglePlusAccessToken Public Property access_token() As String Get Return m_access_token End Get Set(value As String) m_access_token = value End Set End Property Private m_access_token As String Public Property token_type() As String Get Return m_token_type End Get Set(value As String) m_token_type = value End Set End Property Private m_token_type As String Public Property expires_in() As Integer Get Return m_expires_in End Get Set(value As Integer) m_expires_in = value End Set End Property Private m_expires_in As Integer Public Property id_token() As String Get Return m_id_token End Get Set(value As String) m_id_token = value End Set End Property Private m_id_token As String Public Property refresh_token() As String Get Return m_refresh_token End Get Set(value As String) m_refresh_token = value End Set End Property Private m_refresh_token As String End Class Private Async Sub getgoogleplususerdataSer(access_token As String) Try Dim client As New HttpClient() Dim urlProfile = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" & access_token client.CancelPendingRequests() Dim output As HttpResponseMessage = Await client.GetAsync(urlProfile) If output.IsSuccessStatusCode Then Dim outputData As String = Await output.Content.ReadAsStringAsync() Dim serStatus As GoogleUserOutputData = JsonConvert.DeserializeObject(Of GoogleUserOutputData)(outputData) ' You will get the user information here. If serStatus IsNot Nothing Then Session("Username") = serStatus.email End If Response.Redirect("Home.aspx") 'If Session("Username").ToString().Contains("@student.mes.ac.in") = False Then ' Dim script As String = "alert('Invalid user " & Session("Username") & ". Logout your current google session and login with your mes student account.'); window.location=('Login.aspx');" ' Page.ClientScript.RegisterStartupScript(Me.[GetType](), "Redirect", script, True) 'Else ' Response.Redirect("Home.aspx") 'End If End If 'catching the exception Catch ex As Exception End Try End Sub Public Class GoogleUserOutputData Public Property id() As String Get Return m_id End Get Set(value As String) m_id = value End Set End Property Private m_id As String Public Property name() As String Get Return m_name End Get Set(value As String) m_name = value End Set End Property Private m_name As String Public Property email() As String Get Return m_email End Get Set(value As String) m_email = value End Set End Property Private m_email As String End Class End Class