Multi Tenancy Identifier for Office 365
When creating Multi Tenancy environment, there is always a identify the Tenant Id, so that corresponding data could be stored and fetched from Datasource.
In TokenHelper.cs there is a public method GetRealmFromTargetUrl(Uri targetApplicationUri) that takes URI of the target sharepoint site as its parameter. This method actually return a string representation of the realm GUID
Realm is unique to each tenant in Office 365 or to each SharePoint farm on-premises. It is possible to discover the realm at run time. So, it is not necessary to cache this information between requests, but it will cost you an extra round trip to SharePoint each time you want to look it up. If you use code similar toTokenHelper.GetRealmFromTargetUrl with the site URL, and cache the result per site (or even per site and per user), you can use this later without making the extra call.
var tenantAdminUri = new Uri(String.Format("https://{0}-admin.sharepoint.com", tenantStr));
string realm = TokenHelper.GetRealmFromTargetUrl(tenantAdminUri);
You can use Guid now to identify the Tenant.