private void LoadSettings() { var openId = WebUtils.ParseStringFromQueryString("facebookid", string.Empty); var userGuid = Guid.Empty; if (openId.Length > 0) { userGuid = SiteUser.GetUserGuidFromOpenId(siteSettings.SiteId, openId); } else { openId = WebUtils.ParseStringFromQueryString("googleid", string.Empty); if (openId.Length > 0) { userGuid = SiteUser.GetUserGuidFromOpenId(siteSettings.SiteId, openId); } } if (userGuid != Guid.Empty) { siteUser = new SiteUser(siteSettings, userGuid); if (siteUser == null || siteUser.UserId <= 0) { siteUser = null; } } }
protected void OpenIdLogin1_LoggedIn(object sender, OpenIdEventArgs e) { // prevent the base control from doing forms auth for us e.Cancel = true; Guid userGuid = SiteUser.GetUserGuidFromOpenId( siteSettings.SiteId, e.ClaimedIdentifier.ToString()); if (userGuid == Guid.Empty) { // if enough info is available auto create user DoNewUserLogic(e); } else { DoExistingUserLogic(userGuid); } }
private void ProcessToken() { OpenIdRpxHelper rpxHelper = new OpenIdRpxHelper(rpxApiKey, rpxBaseUrl); OpenIdRpxAuthInfo authInfo = rpxHelper.AuthInfo(authToken, tokenUrl); if ((authInfo == null) || (!authInfo.IsValid)) { log.Debug($"openid-debug: authInfo is null or authInfo.IsValid='false' "); Response.Redirect(SiteRoot + "/Secure/Login.aspx"); return; } if (Request.IsAuthenticated) { log.Debug($"openid-debug: authInfo is valid and user exists, authenticated "); HandleAuthenticatedUser(rpxHelper, authInfo); return; } Guid userGuid = Guid.Empty; SiteUser user = null; //first find a site user by email // this allows associating the openid user with an existing user. if ((authInfo.Email.Length > 0)) { log.Debug($"openid-debug: found user by email "); user = SiteUser.GetByEmail(siteSettings, authInfo.Email); } if (authInfo.PrimaryKey.Length == 36) { try { userGuid = new Guid(authInfo.PrimaryKey); } catch (FormatException) { } catch (OverflowException) { } } if ((user == null) && (userGuid == Guid.Empty)) { userGuid = SiteUser.GetUserGuidFromOpenId( siteSettings.SiteId, authInfo.Identifier); } if ((user == null) && (userGuid != Guid.Empty)) { user = new SiteUser(siteSettings, userGuid); if (WebConfigSettings.UseRelatedSiteMode) { if (user.UserId == -1) { user = null; log.Debug($"openid-debug: user not found "); } } else if (user.SiteGuid != siteSettings.SiteGuid) { user = null; log.Debug($"openid-debug: user not connected to this site ({siteSettings.SiteId.ToString()}) "); } } if (user == null) { // not an existing user if (siteSettings.AllowNewRegistration) { HandleNewUser(rpxHelper, authInfo); } else { log.Debug($"openid-debug: user not found, AllowNewRegistrations='false' "); WebUtils.SetupRedirect(this, SiteRoot); return; } } else { log.Debug($"openid-debug: user found ({user.LoweredEmail}, {user.UserId.ToString()}) "); bool needToSave = false; if ((siteSettings.UseSecureRegistration) && (user.RegisterConfirmGuid != Guid.Empty)) { if (authInfo.VerifiedEmail.Length > 0) { user.SetRegistrationConfirmationGuid(Guid.Empty); user.Email = authInfo.VerifiedEmail; needToSave = true; } } if (user.OpenIdUri.Length == 0) { user.OpenIdUri = authInfo.Identifier; needToSave = true; } if (needToSave) { user.Save(); } if (WebConfigSettings.OpenIdRpxUseMappings) { if ((authInfo.PrimaryKey.Length == 0) || (authInfo.PrimaryKey != user.UserGuid.ToString())) { rpxHelper.Map(authInfo.Identifier, user.UserGuid.ToString()); } } SignInUser(user, false); } }
private void Page_Load(object sender, System.EventArgs e) { SecurityHelper.DisableBrowserCache(); LoadSettings(); if ( facebookAppId.Length == 0 || facebookAppSecret.Length == 0 || redirectUri.Length == 0 ) { FailureText.Text = "Không thể đăng nhập bằng Facebook vì chưa được cấu hình."; //SiteUtils.RedirectToHomepage(); return; } var facebookVersion = "v2.7"; var url = string.Format("https://www.facebook.com/{0}/dialog/oauth?client_id={1}&redirect_uri={2}&state={3}&sdk=php-sdk-4.0.23&scope=email,public_profile,user_friends", facebookVersion, facebookAppId, redirectUri, Guid.NewGuid().ToString().ToLower()); if (facebookCode.Length == 0) { WebUtils.SetupRedirect(this, url); return; } // Get access token info string accessTokenUri = string.Format("https://graph.facebook.com/oauth/access_token?client_id={0}&redirect_uri={1}&client_secret={2}&code={3}", facebookAppId, redirectUri, facebookAppSecret, facebookCode); try { string response = HttpGet(accessTokenUri); if (response.Length > 0) { // Get access token string responseToken = response.Split('&')[0]; if (responseToken.Contains("access_token=")) { string accessToken = responseToken.Replace("access_token=", ""); // Get clientNo infomation var userData = HttpGet(string.Format("https://graph.facebook.com/{0}/me?access_token={1}", facebookVersion, accessToken)); var oUser = new JavaScriptSerializer().Deserialize <FaceBookUser>(userData); if (oUser != null) { var userGuid = Guid.Empty; if (oUser.Id.Length > 0) { userGuid = SiteUser.GetUserGuidFromOpenId(siteSettings.SiteId, oUser.Id); } SiteUser user = null; if (userGuid != Guid.Empty) { user = new SiteUser(siteSettings, userGuid); if (user == null || user.UserId <= 0) { user = CreateUser(oUser); } } else { user = CreateUser(oUser); } if (user != null && user.UserId > 0 && user.ApprovedForLogin) { DoUserLogin(user); SetupScripts(); } else { RedirectToUpdatePassword(oUser.Id); } } } } } catch (Exception ex) { log.Error(ex.Message); } }
private void Page_Load(object sender, System.EventArgs e) { SecurityHelper.DisableBrowserCache(); LoadSettings(); if ( googleClientId.Length == 0 || googleSecret.Length == 0 || redirectUri.Length == 0 ) { FailureText.Text = "Không thể đăng nhập bằng Google vì chưa được cấu hình."; //SiteUtils.RedirectToHomepage(); return; } var url = string.Format("https://accounts.google.com/o/oauth2/auth?scope={0}&state=1&redirect_uri={1}&client_id={2}&response_type=code&approval_prompt=auto&access_type=online", Server.UrlEncode("https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"), redirectUri, googleClientId); if (code.Length == 0) { WebUtils.SetupRedirect(this, url); return; } try { var tokenUrl = "https://accounts.google.com/o/oauth2/token"; var postData = string.Format("code={0}&client_id={1}&client_secret={2}&redirect_uri={3}&grant_type=authorization_code", code, googleClientId, googleSecret, redirectUri); string response = HttpPost(tokenUrl, postData); if (response.Length > 0) { // Get access token var oToken = (new JavaScriptSerializer()).Deserialize <GoogleToken>(response); string profileUrl = "https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + oToken.access_token; string result = HttpGet(profileUrl); //and Deserialize the JSON response var oUser = (new JavaScriptSerializer()).Deserialize <GoogleUser>(result); if (oUser != null) { var userGuid = Guid.Empty; if (oUser.Id.Length > 0) { userGuid = SiteUser.GetUserGuidFromOpenId(siteSettings.SiteId, oUser.Id); } SiteUser user = null; if (userGuid != Guid.Empty) { user = new SiteUser(siteSettings, userGuid); if (user == null || user.UserId <= 0) { user = CreateUser(oUser); } } else { user = CreateUser(oUser); } if (user != null && user.UserId > 0 && user.ApprovedForLogin) { DoUserLogin(user); SetupScripts(); } else { RedirectToUpdatePassword(oUser.Id); } } } } catch (Exception ex) { log.Error(ex.Message); } }
private void ProcessToken() { OpenIdRpxHelper rpxHelper = new OpenIdRpxHelper(rpxApiKey, rpxBaseUrl); OpenIdRpxAuthInfo authInfo = rpxHelper.AuthInfo(authToken, tokenUrl); if ((authInfo == null) || (!authInfo.IsValid)) { Response.Redirect(SiteRoot + "/Secure/Login.aspx"); return; } if (Request.IsAuthenticated) { HandleAuthenticatedUser(rpxHelper, authInfo); return; } Guid userGuid = Guid.Empty; SiteUser user = null; //first find a site user by email if ((authInfo.Email.Length > 0)) { user = SiteUser.GetByEmail(siteSettings, authInfo.Email); } if (authInfo.PrimaryKey.Length == 36) { try { userGuid = new Guid(authInfo.PrimaryKey); } catch (FormatException) { } catch (OverflowException) { } } if ((user == null) && (userGuid == Guid.Empty)) { userGuid = SiteUser.GetUserGuidFromOpenId( siteSettings.SiteId, authInfo.Identifier); } if ((user == null) && (userGuid != Guid.Empty)) { user = new SiteUser(siteSettings, userGuid); if (user.SiteGuid != siteSettings.SiteGuid) { user = null; } } if (user == null) { // not an existing user if (siteSettings.AllowNewRegistration) { HandleNewUser(rpxHelper, authInfo); } else { WebUtils.SetupRedirect(this, SiteRoot); return; } } else { bool needToSave = false; if ((siteSettings.UseSecureRegistration) && (user.RegisterConfirmGuid != Guid.Empty)) { if (authInfo.VerifiedEmail.Length > 0) { user.SetRegistrationConfirmationGuid(Guid.Empty); user.Email = authInfo.VerifiedEmail; needToSave = true; } } if (user.OpenIdUri.Length == 0) { user.OpenIdUri = authInfo.Identifier; needToSave = true; } if (needToSave) { user.Save(); } if (WebConfigSettings.OpenIdRpxUseMappings) { if ((authInfo.PrimaryKey.Length == 0) || (authInfo.PrimaryKey != user.UserGuid.ToString())) { rpxHelper.Map(authInfo.Identifier, user.UserGuid.ToString()); } } SignInUser(user); } }