Retrieve IP Address of a Community User
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() returns all valuable information in key pair as below
{
"NumSecondsValid": "7200",
"LastModifiedDate": "Wed Feb 24 12:41:13 GMT 2021",
"CreatedDate": "Wed Feb 24 12:41:13 GMT 2021",
"LoginGeoId": "04F4W00007B3IkEUAV",
"LoginHistoryId": "0Ya4W0000ALoOkESQV",
"LoginDomain": "lb2bdcm-developer-edition.na139.force.com",
"LogoutUrl": null,
"ParentId": null,
"SessionId": "0Ak4W0000pPf1ytSAB",
"SessionSecurityLevel": "STANDARD",
"SourceIp": "99.48.55.154",
"LoginType": "Chatter Communities External User",
"UserType": "PowerCustomerSuccess",
"SessionType": "ChatterNetworks",
"Username": "iammike@gmail.com.bolt",
"UsersId": "0054W00000Cdxf7QAB"
}
Comments
Post a Comment