public async Task AddLogin(IdentityUserLogin <Guid> login)
        {
            if (Exists && login != null)
            {
                _data.State.Logins.Add(login);
                await GrainFactory.AddOrUpdateToLookup(login.LoginProvider, login.ProviderKey, _id);

                await _data.WriteStateAsync();
            }
        }
        public async Task <IdentityResult> Update(TUser user)
        {
            if (_data.State.User == null)
            {
                return(IdentityResult.Failed(_errorDescriber.DefaultError()));
            }
            if (string.IsNullOrEmpty(user.Email))
            {
                return(IdentityResult.Failed(_errorDescriber.InvalidEmail(user.Email)));
            }
            if (string.IsNullOrEmpty(user.UserName))
            {
                return(IdentityResult.Failed(_errorDescriber.InvalidUserName(user.UserName)));
            }

            // Make sure to set normalized username and email
            var newEmail    = _normalizer.NormalizeEmail(user.Email);
            var newUsername = _normalizer.NormalizeName(user.UserName);

            if (newEmail != _data.State.User.NormalizedEmail && !await GrainFactory.AddOrUpdateToLookup(OrleansIdentityConstants.EmailLookup, newEmail, _id))
            {
                return(IdentityResult.Failed(_errorDescriber.DuplicateEmail(newEmail)));
            }

            if (newUsername != _data.State.User.NormalizedUserName && !await GrainFactory.AddOrUpdateToLookup(OrleansIdentityConstants.UsernameLookup, newUsername, _id))
            {
                // if email changed, then undo that change
                if (newEmail != _data.State.User.NormalizedEmail)
                {
                    await GrainFactory.SafeRemoveFromLookup(OrleansIdentityConstants.EmailLookup, user.NormalizedEmail, _id);
                }

                return(IdentityResult.Failed(_errorDescriber.DuplicateUserName(user.UserName)));
            }

            // Remove old values
            if (newEmail != _data.State.User.NormalizedEmail)
            {
                await GrainFactory.RemoveFromLookup(OrleansIdentityConstants.EmailLookup, _data.State.User.NormalizedEmail);
            }

            if (newUsername != _data.State.User.NormalizedUserName)
            {
                await GrainFactory.RemoveFromLookup(OrleansIdentityConstants.UsernameLookup, _data.State.User.NormalizedUserName);
            }

            _data.State.User = user;

            await _data.WriteStateAsync();

            return(IdentityResult.Success);
        }
Пример #3
0
        public async Task <IdentityResult> Update(TRole role)
        {
            if (!Exists || string.IsNullOrEmpty(role.Name))
            {
                return(IdentityResult.Failed());
            }

            // Normalize name
            var newRoleName = _normalizer.NormalizeName(role.Name);

            if (newRoleName != _data.State.Role.NormalizedName && !await GrainFactory.AddOrUpdateToLookup(OrleansIdentityConstants.RoleLookup, newRoleName, _id))
            {
                return(IdentityResult.Failed());
            }

            _data.State.Role = role;
            await _data.WriteStateAsync();

            return(IdentityResult.Success);
        }
        public async Task <IdentityResult> Create(TUser user)
        {
            if (Exists)
            {
                return(IdentityResult.Failed(_errorDescriber.LoginAlreadyAssociated()));
            }
            if (string.IsNullOrEmpty(user.Email))
            {
                return(IdentityResult.Failed(_errorDescriber.InvalidEmail(user.Email)));
            }
            if (string.IsNullOrEmpty(user.UserName))
            {
                return(IdentityResult.Failed(_errorDescriber.InvalidUserName(user.UserName)));
            }

            // Make sure to set normalized username and email
            user.NormalizedEmail    = _normalizer.NormalizeEmail(user.Email);
            user.NormalizedUserName = _normalizer.NormalizeName(user.UserName);

            if (!await GrainFactory.AddOrUpdateToLookup(OrleansIdentityConstants.EmailLookup, user.NormalizedEmail, _id))
            {
                return(IdentityResult.Failed(_errorDescriber.DuplicateEmail(user.Email)));
            }

            if (!await GrainFactory.AddOrUpdateToLookup(OrleansIdentityConstants.UsernameLookup, user.NormalizedUserName, _id))
            {
                await GrainFactory.SafeRemoveFromLookup(OrleansIdentityConstants.EmailLookup, user.NormalizedEmail, _id);

                return(IdentityResult.Failed(_errorDescriber.DuplicateUserName(user.UserName)));
            }

            _data.State.User = user;

            await _data.WriteStateAsync();

            return(IdentityResult.Success);
        }