public async Task<User> SaveUser(User user)
        {
            User savedUser = null;

            entities.User dbUser = new entities.User { Claims = user.Claims, DisplayName = user.DisplayName, Email = user.Email, UserName = user.UserName };
            if(user.Id != Guid.Empty)
            {
                dbUser.Id = user.Id;
                var result = await users.ReplaceOneAsync(
                    filter: new BsonDocument("_id", dbUser.Id),
                    replacement: dbUser);
                if (result.IsAcknowledged == false || result.ModifiedCount == 0 || result.ModifiedCount > 1)
                    throw new InvalidOperationException("Could not update the user! " + user);
            }
            else
            {
                await users.InsertOneAsync(dbUser);
            }

            savedUser = new User
            {
                Claims = new List<string>(dbUser.Claims),
                DisplayName = dbUser.DisplayName,
                Email = dbUser.Email,
                Id = dbUser.Id,
                UserName = dbUser.UserName
            };

            return savedUser;
        }
Exemplo n.º 2
0
 public UserIdentity(User user)
 {
     this.Claims = user.Claims;
     this.DisplayName = user.DisplayName;
     this.Email = user.Email;
     this.Id = user.Id;
     this.UserName = user.UserName;
 }
        public async Task<User> FindUserByEmail(string email)
        {
            User user = null;

            var u = await users.FindAsync(a => a.Email.ToLower() == email.ToLower());
            if(u != null)
            {
                var userList = await u.ToListAsync();
                if (userList != null && userList.Count > 0)
                {
                    var dbUser = userList[0];
                    user = new User
                    {
                        Claims = new List<string>(dbUser.Claims),
                        DisplayName = dbUser.DisplayName,
                        Email = dbUser.Email,
                        Id = dbUser.Id,
                        UserName = dbUser.UserName
                    };
                }
            }
            
            return user;
        }