Exemple #1
0
        public async Task <IActionResult> ExternalLoginCallback(string returnUrl = null, string remoteError = null)
        {
            if (remoteError != null)
            {
                ModelState.AddModelError(string.Empty, $"Error from external provider: {remoteError}");
                return(View(nameof(Login)));
            }
            var info = await signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(RedirectToAction(nameof(Login)));
            }

            var loginProvider = info.LoginProvider;
            // Sign in the user with this external login provider if the user already has a login.
            var user = await userManager.FindByLoginAsync(loginProvider, info.ProviderKey);

            var result = await signInManager.ExternalLoginSignInAsync(loginProvider, info.ProviderKey, false);

            if (result.Succeeded)
            {
                return(RedirectToLocal(returnUrl));
            }
            if (result.RequiresTwoFactor)
            {
                //    return RedirectToAction(nameof(SendCode), new { ReturnUrl = returnUrl });
            }
            if (result.IsLockedOut)
            {
                return(View("Lockout"));
            }
            else
            {
                var reqFields = tenantContext.Items[Tenant.Constants.REQUIREDFIELDS] as Field[];
                var optFields = tenantContext.Items[Tenant.Constants.OPTIONALFIELDS] as Field[];

                var vm = new ExternalLoginConfirmationViewmodel
                {
                    Email     = info.Principal.FindFirstValue(ClaimTypes.Email),
                    ReturnUrl = returnUrl,
                    Required  = reqFields.Select(x => new ValueField(x)
                    {
                        Value = info.Principal.FindFirstValue(x.ClaimType)
                    }).ToList(),
                    Optional = optFields.Select(x => new ValueField(x)
                    {
                        Value = info.Principal.FindFirstValue(x.ClaimType)
                    }).ToList()
                };

                // If the user does not have an account, then ask the user to create an account.
                return(View("ExternalLoginConfirmation", vm));
            }
        }
Exemple #2
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewmodel model, string returnUrl = null)
        {
            if (ModelState.IsValid && model.Required.All(x => !string.IsNullOrEmpty(x.Value)))
            {
                var info = await signInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }

                var user = new ApplicationUser(Guid.NewGuid())
                {
                    UserName = Guid.NewGuid().ToString(),
                    Email    = model.Email
                };

                var result = await userManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    await userManager.AddLoginAsync(user, info);

                    var claims = new List <Claim>();
                    claims.Add(info.Principal.FindFirst(ClaimTypes.NameIdentifier));

                    foreach (var required in model.Required.Where(x => !string.IsNullOrEmpty(x.Value)))
                    {
                        claims.Add(new Claim(required.ClaimType, required.Value));
                    }
                    foreach (var optional in model.Optional.Where(x => !string.IsNullOrEmpty(x.Value)))
                    {
                        claims.Add(new Claim(optional.ClaimType, optional.Value));
                    }

                    var phoneClaim = claims.Find(x => x.Type.Equals(ClaimTypes.MobilePhone));
                    if (phoneClaim != default(Claim))
                    {
                        await userManager.SetPhoneNumberAsync(user, phoneClaim.Value);
                    }
                    await userManager.AddClaimsAsync(user, claims);

                    // Send an email with this link
                    var code = await userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code }, protocol: HttpContext.Request.Scheme);
                    await emailSender.SendEmailAsync(model.Email, $"Welkom bij {tenantContext.Name}", $"Om gebruik te maken van uw {tenantContext.Name} account moet u uw email adres bevestigen. Gebruik daarvoor deze link: <a href='{callbackUrl}'>Activeer nu</a>");

                    return(View("Registered"));
                }
                AddErrors(result);
            }
            return(View(model));
        }