Skip to content

Instantly share code, notes, and snippets.

@dealproc
Created March 17, 2019 07:05
Show Gist options
  • Save dealproc/9ee03bff9a2eadb253896b8dd360cfed to your computer and use it in GitHub Desktop.
Save dealproc/9ee03bff9a2eadb253896b8dd360cfed to your computer and use it in GitHub Desktop.
Multi-Tenant Solutions - HostnameAccountAccessor.cs
public class HostnameAccountAccessor : IAccountAccessor {
static readonly ILog _log = LogProvider.For<HostnameAccountAccessor>();
readonly IAccountRepository _AccountRepository;
readonly IOwinContext _owinContext;
Guid _instanceID;
public Guid InstanceID { get { return _instanceID; } }
public HostnameAccountAccessor(IOwinContext owinContext, IAccountRepository accountRepository) {
_instanceID = Guid.NewGuid();
_owinContext = owinContext;
_AccountRepository = accountRepository;
}
public Account GetCurrentAccount() {
if (_owinContext == null) { return null; }
var requestUri = _owinContext.Request.Uri;
if (requestUri == null) {
throw new UnknownAccountException(1, "Could not resolve hostname from HttpContext.Current.");
}
_log.DebugFormat("Host name resolved from HttpContext.Current == {hostname}", requestUri.Host);
var account = _AccountRepository.GetFirst(x => x.Hostname == requestUri.Host);
if (account == null) {
_log.Debug("Account was not found in system.");
throw new UnknownAccountException(2, string.Format("Hostname: {0} has not been registered.", requestUri.Host));
}
_log.DebugFormat("Associated Account Id/GUID: {accountId}/{accountGuid}", account.Id, account.GUID);
return account;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment