コード例 #1
0
        private async Task <Tuple <IdentityResult, Guid> > CreateUser(RegisterAccountRequest model)
        {
            InviteInfo invite = null;

            if (model.InviteCode != null)
            {
                invite = _invitesService.FindInviteByToken(model.InviteCode.Value);
                if (invite == null || !invite.Email.Equals(model.Email, StringComparison.OrdinalIgnoreCase))
                {
                    return(new Tuple <IdentityResult, Guid>(IdentityResult.Failed("Bad invite"), Guid.Empty));
                }
            }

            var user = new ApplicationUser
            {
                DomainId = Guid.NewGuid(),
                UserName = model.Email,
                Email    = model.Email,
                IsActive = true,
                Profile  = new ApplicationUserProfile {
                    FirstName = model.FirstName, LastName = model.LastName
                }
            };

            IdentityResult result = await _userManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                return(new Tuple <IdentityResult, Guid>(result, Guid.Empty));
            }

            string role = GlobalInfo.Candidate;

            if (invite != null)
            {
                role = invite.RoleName;
                user.EmailConfirmed = true;
                await _invitesService.UseInviteAsync(model.InviteCode.Value, user.Id);
            }
            else
            {
                await SendConfirmEmailAsync(user);
            }

            await _userManager.AddToRoleAsync(user.Id, role);

            return(new Tuple <IdentityResult, Guid>(IdentityResult.Success, user.DomainId));
        }
コード例 #2
0
        public async Task <IHttpActionResult> VerifyInviteAsync([FromUri] Guid token)
        {
            try
            {
                var invite = _invitesService.FindInviteByToken(token);

                if (invite == null)
                {
                    return(Redirect(new Uri(GlobalInfo.InviteNotFoundUrl)));
                }

                if (_invitesService.IsExpired(token))
                {
                    return(Redirect(new Uri(GlobalInfo.InviteIsExpiredUrl)));
                }

                var userId = (await _applicationUserManager.FindByEmailAsync(invite.Email))?.Id;

                if (userId != null)
                {
                    var isInRole = await _applicationUserManager.IsInRoleAsync(userId, invite.RoleName);

                    if (isInRole)
                    {
                        return(Redirect(new Uri(GlobalInfo.UserAlreadyInRoleUrl)));
                    }
                    else
                    {
                        await _applicationUserManager.AddToRoleAsync(userId, invite.RoleName);

                        await _invitesService.UseInviteAsync(token, userId);

                        return(Redirect(new Uri(GlobalInfo.UserAddedToRoleUrl)));
                    }
                }

                return(Redirect(new Uri($"{GlobalInfo.RegisterByInviteUrl}{token}")));
            }
            catch (Exception)
            {
                return(Redirect(new Uri(GlobalInfo.InviteError)));
            }
        }