Esempio n. 1
0
        public async Task <bool> UpdateAsync(Domains.ResourceOwner resourceOwner)
        {
            using (var transaction = await _context.Database.BeginTransactionAsync().ConfigureAwait(false))
            {
                try
                {
                    var record = await _context.ResourceOwners
                                 .Include(r => r.Claims)
                                 .FirstOrDefaultAsync(r => r.Id == resourceOwner.Id).ConfigureAwait(false);

                    if (record == null)
                    {
                        return(false);
                    }

                    record.Password                = resourceOwner.Password;
                    record.IsLocalAccount          = resourceOwner.IsLocalAccount;
                    record.TwoFactorAuthentication = (int)resourceOwner.TwoFactorAuthentication;
                    _context.ResourceOwnerClaims.RemoveRange(record.Claims);
                    if (resourceOwner.Claims != null)
                    {
                        foreach (var claim in resourceOwner.Claims)
                        {
                            record.Claims.Add(new ResourceOwnerClaim
                            {
                                Id = Guid.NewGuid().ToString(),
                                ResourceOwnerId = record.Id,
                                ClaimCode       = claim.Type,
                                Value           = claim.Value
                            });
                        }
                    }

                    await _context.SaveChangesAsync().ConfigureAwait(false);

                    transaction.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    _managerEventSource.Failure(ex);
                    transaction.Rollback();
                    return(false);
                }
            }
        }
Esempio n. 2
0
        public async Task <bool> InsertAsync(Domains.ResourceOwner resourceOwner)
        {
            try
            {
                var user = new ResourceOwner
                {
                    Id                      = resourceOwner.Id,
                    Password                = resourceOwner.Password,
                    IsLocalAccount          = resourceOwner.IsLocalAccount,
                    TwoFactorAuthentication = (int)resourceOwner.TwoFactorAuthentication,
                    Claims                  = new List <ResourceOwnerClaim>()
                };

                if (resourceOwner.Claims != null)
                {
                    foreach (var claim in resourceOwner.Claims)
                    {
                        user.Claims.Add(new ResourceOwnerClaim
                        {
                            Id = Guid.NewGuid().ToString(),
                            ResourceOwnerId = user.Id,
                            ClaimCode       = claim.Type,
                            Value           = claim.Value
                        });
                    }
                }

                _context.ResourceOwners.Add(user);
                await _context.SaveChangesAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                _managerEventSource.Failure(ex);
                return(false);
            }

            return(true);
        }