public Task<User> GetByLoginAsync(Login login, CancellationToken cancellationToken = default(CancellationToken))
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (login == (Login)null)
         throw new ArgumentNullException("login");
     return this._userRepository.GetFirstOrDefaultAsync(x => x.Logins != null && x.Logins.Any(l => l.Id == login.Id || l.ProviderKey == login.ProviderKey), cancellationToken);
 }
        public async Task AddLoginAsync(User user, Login login, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (login == (Login)null)
                throw new ArgumentNullException("login");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), new CancellationToken(), (x => x.Logins));

            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));

            if (dbUser.Logins == null)
                dbUser.Logins = new List<Login>();

            dbUser.Logins.Add(login);

            this._userRepository.SetModified(dbUser);


            await this._userRepository.UnitOfWork.CommitAsync();
        }
        public async Task RemoveLoginAsync(User user, Login login, CancellationToken cancellationToken = default(CancellationToken))
        {
            cancellationToken.ThrowIfCancellationRequested();
            if (user == (User)null)
                throw new ArgumentNullException("user");
            if (login == (Login)null)
                throw new ArgumentNullException("login");
            User dbUser = await this._userRepository.GetFirstOrDefaultAsync((x => x.Id == user.Id), cancellationToken, (x => x.Logins));
            if (dbUser == (User)null)
                throw new Exception(string.Format(Messages.UserNotFoundException, ("User id=" + user.Id)));

            if (dbUser.Logins != null)
            {
                Login log = (Login)null;
                log = (login.Id == Guid.Empty) ?
                    dbUser.Logins.FirstOrDefault(x => x.ProviderKey != null && x.ProviderKey == login.ProviderKey && x.LoginProvider != null && x.LoginProvider == login.LoginProvider) :
                    dbUser.Logins.FirstOrDefault(x => x.Id == login.Id);
                if (log == (Login)null)
                    throw new Exception(Messages.RemoveNonExistingException);
                dbUser.Logins.Remove(log);
                this._userRepository.SetModified(dbUser);

                await this._userRepository.UnitOfWork.CommitAsync();
            }
        }
 public static Login ToDomainLogin(this UserLoginInfo model)
 {
     Login login = (Login)null;
     if (model != null)
         login = new Login()
         {
             LoginProvider = model.LoginProvider,
             ProviderKey = model.ProviderKey,
             ProviderDisplayName = model.ProviderKey
         };
     return login;
 }