daily pastebin goal
13%
SHARE
TWEET

Untitled

a guest Mar 22nd, 2019 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class ClaimsPrincipalAccountAccessor : IAccountAccessor {
  2.   static readonly ILog _log = LogProvider.For<ClaimsPrincipalAccountAccessor>();
  3.  
  4.   readonly IAccountRepository _AccountRepository;
  5.   readonly IOwinContext _owinContext;
  6.  
  7.   public ClaimsPrincipalAccountAccessor(IOwinContext owinContext, IAccountRepository accountRepository) {
  8.     _owinContext = owinContext;
  9.     _AccountRepository=accountRepository;
  10.   }
  11.  
  12.   public DataModel.Users.Account GetCurrentAccount() {
  13.     if (_owinContext == null) {
  14.       throw new NotImplementedException("Owin context is not available.");
  15.     }
  16.     var principal = _owinContext.Authentication.User;
  17.     if (principal == null) {
  18.       throw new UnauthorizedAccessException("User is unauthorized.".ShouldBeAResource());
  19.     }
  20.  
  21.     try {
  22.       var accountClaim = principal.Claims.First(x => x.Type == Constants.ClaimTypes.TenantId || x.Type == Constants.ClaimTypes.AssociatedAccount);
  23.       if (accountClaim == null) {
  24.         _log.Debug("Principal does not have a TenantId or Associated Account claim.");
  25.         throw new UnknownAccountException(4, "Principal does not have a TenantId or Associated Account claim.".ShouldBeAResource());
  26.       }
  27.  
  28.       var account = _AccountRepository.GetFirst(x => x.GUID == accountClaim.Value);
  29.       if (account == null) {
  30.         _log.Debug("Account was not found in the system.");
  31.         throw new UnknownAccountException(3, "Account cannot be found from hardware's claims.".ShouldBeAResource());
  32.       };
  33.  
  34.       _log.DebugFormat("Associated Account Id/GUID: {accountId}/{accountGuid}", account.Id, account.GUID);
  35.       return account;
  36.       } catch (Exception) {
  37.         _log.Debug("User is not a hardware device.  Throwing 'null' back to calling method.");
  38.         return null;
  39.       }
  40.     }
  41.   }
  42. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top