Пример #1
0
        public async Task<HttpResponseMessage> ConfirmEmail(string aspUserId, string code)
        {
            AuthRepository repo = new AuthRepository();

            try
            {
                if (aspUserId != null && code != null)
                {
                    IdentityResult result = await repo.ConfirmEmail(aspUserId, code);

                    if (result.Succeeded)
                    {
                        //If email confirmation succeeds, automatically give the a 2FA token
                        ApplicationUser user = await repo.FindAspUserByUserId(aspUserId);
                        var userIdentity = await user.GenerateUserIdentityAsync(UserManager);

                        string pinCode = await repo.GetTwoFactorCode(aspUserId);
                        await repo.VerifyTwoFactorToken(aspUserId, pinCode);

                        var rememberBrowserIdentity = repo.authManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
                        repo.authManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity, rememberBrowserIdentity);

                        var response = Request.CreateResponse(HttpStatusCode.Moved);
                        response.Headers.Location = new Uri(Config.BaseWWClientURL + "#/registerConfirm");

                        return response;
                    }
                }

                var errorResponse = Request.CreateResponse(HttpStatusCode.Moved);
                errorResponse.Headers.Location = new Uri(Config.BaseWWClientURL + "/#/registerError?id=" + aspUserId);
                return errorResponse;
            }
            catch (Exception ex)
            {
                ErrorLogging.LogError(ex);

                var errorResponse = Request.CreateResponse(HttpStatusCode.Moved);
                errorResponse.Headers.Location = new Uri(Config.BaseWWClientURL + "/#/registerError?id=" + aspUserId);
                return errorResponse;
            }
            finally
            {
                repo.Dispose();
            }
        }
Пример #2
0
        public async Task<IHttpActionResult> Verify2FACode(string userName, string pinCode)
        {
            try
            {
                using (AuthRepository repo = new AuthRepository())
                {
                    ApplicationUser user = await repo.FindUserByName(userName);
                    bool isCodeValid = await repo.VerifyTwoFactorToken(user.Id, pinCode);

                    ClaimsIdentity userIdentity = await user.GenerateUserIdentityAsync(UserManager);

                    if (isCodeValid)
                    {
                        var rememberBrowserIdentity = repo.authManager.CreateTwoFactorRememberBrowserIdentity(user.Id);
                        repo.authManager.SignIn(new AuthenticationProperties { IsPersistent = true }, userIdentity, rememberBrowserIdentity);
                    }
                    else
                    {
                        return BadRequest("Sorry, that is not a valid code.");
                    }

                }
                return Ok();
            }
            catch (Exception ex)
            {
                Helper.ErrorLogging.LogError(ex);
                return InternalServerError(ex);
            }
        }