Posts

Showing posts from February, 2021

Retrieve Guest User IP Address with VF Page

Image
The two standard objects:  AuthSession ,  LoginGeo don’t store the login information of a Guest User. Using a VF Page  <apex:page showHeader="false" sidebar="false" controller="userIPAddressController"  action="{!getUserIPAddress}">  <script>     setTimeout(function(){         var message = '{!ip}';         parent.postMessage(message, window.top.location.origin);               }, 3000);  </script>  <apex:form >      {!ip}  </apex:form> </apex:page> Controller public class userIPAddressController{     public string ip {get;set;}     public pageReference getUserIPAddress() {                  // True-Client-IP has the value when the request is coming via the caching integration.         ip = ApexPages.currentPage().getHeaders()...

Retrieve IP Address of a Community User

Image
For a variety of purposes, including displaying items, sending texts, and other security-related tasks, tracing IP addresses is necessary. Logged in User IP Address Getting IP Address for logged in user is relatively simple as Salesforce stores this information in two standard objects:  AuthSession ,  LoginGeo . List<AuthSession> auth = [SELECT Id , IsCurrent, SessionType, CreatedDate, LoginGeoId, NumSecondsValid FROM AuthSession WHERE UsersId =: Userinfo.getUserID()]; if(auth.isEmpty() == false && auth[0].IsCurrent ){ system.debug('yes, it is current user session'); }else{ system.debug('No, it is not current user session'); } List<LoginGeo> lg = [SELECT Id,City, Country, PostalCode, Subdivision, Latitude, Longitude,LoginTime FROM LoginGeo WHERE Id =: auth[0].LoginGeoId]; Alternatively, you can use  Auth.SessionManagement  Class as well as below: Auth.SessionManagement.getCurrentSession().get('LoginGeoId'); getCurrentSession() re...