Inheritance: IdentityUser
Esempio n. 1
0
        public async Task<ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl = null)
        {
            if (User.Identity.IsAuthenticated)
            {
                return RedirectToAction("Index", "Manage");
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await SignInManager.GetExternalLoginInfoAsync(cancellationToken: Context.RequestAborted);
                if (info == null)
                {
                    return View("ExternalLoginFailure");
                }
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, cancellationToken: Context.RequestAborted);

#if TESTING
                //Just for automated testing adding a claim named 'ManageStore' - Not required for production
                var manageClaim = info.ExternalIdentity.Claims.Where(c => c.Type == "ManageStore").FirstOrDefault();
                if (manageClaim != null)
                {
                    await UserManager.AddClaimAsync(user, manageClaim, cancellationToken: Context.RequestAborted);
                }
#endif

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user, info, cancellationToken: Context.RequestAborted);
                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent: false, cancellationToken: Context.RequestAborted);
                        return RedirectToLocal(returnUrl);
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return View(model);
        }
Esempio n. 2
0
        public async Task<IActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
                var result = await UserManager.CreateAsync(user, model.Password, cancellationToken: Context.RequestAborted);
                if (result.Succeeded)
                {
                    //Bug: Remember browser option missing?
                    //Uncomment this and comment the later part if account verification is not needed.
                    await SignInManager.SignInAsync(user, isPersistent: false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    string code = await UserManager.GenerateEmailConfirmationTokenAsync(user, cancellationToken: Context.RequestAborted);
                    var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Context.Request.Scheme);
                    var email = new IdentityMessage
                    {
                        Destination = model.Email,
                        Subject = "Confirm your account",
                        Body = "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>"
                    };
                    await UserManager.SendMessageAsync("Email", email, cancellationToken: Context.RequestAborted);
#if !DEMO
                    return RedirectToAction("Index", "Home");
#else
                    //To display the email link in a friendly page instead of sending email
                    ViewBag.Link = callbackUrl;
                    return View("DemoLinkDisplay");
#endif
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }