Example #1
0
        public async Task <ActionResult> LoginExternoCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login"));
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await SignInManager.ExternalSignInAsync(loginInfo, isPersistent : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                ConfirmacaoLoginExternoViewModel conf = new ConfirmacaoLoginExternoViewModel();
                conf.Email    = loginInfo.Email;
                conf.Provider = loginInfo.Login.LoginProvider;
                conf.Nome     = conf.Email;

                if (conf.Provider == "Facebook")
                {
                    //conf.Nome = loginInfo.ExternalIdentity.Claims.First(c => c.Type == "urn:facebook:name").Value;
                    conf.Nome     = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.Name).Select(c => c.Value).SingleOrDefault();
                    conf.Telefone = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.MobilePhone).Select(c => c.Value).SingleOrDefault();
                }
                else if (conf.Provider == "Google")
                {
                    conf.Nome     = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.Name).Select(c => c.Value).SingleOrDefault();
                    conf.Telefone = loginInfo.ExternalIdentity.Claims.Where(c => c.Type == System.Security.Claims.ClaimTypes.MobilePhone).Select(c => c.Value).SingleOrDefault();
                }
                return(View("ConfirmacaoLoginExterno", conf));
            }
        }
Example #2
0
        public async Task <ActionResult> ConfirmacaoLoginExterno(ConfirmacaoLoginExternoViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Home"));
            }

            if (ModelState.IsValid)
            {
                bool isEmail = Regex.IsMatch(model.Email, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);

                if (!isEmail)
                {
                    ModelState.AddModelError("Email", "Email inválido.");
                }
                else
                {
                    var user = await UserManager.FindByEmailAsync(model.Email);

                    if (user != null)
                    {
                        ModelState.AddModelError("Email", "Este e-mail já está em uso. Por favor utilize outro.");
                    }
                }
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };

                Usuario usu = new Usuario();
                usu.Email    = model.Email;
                usu.Nome     = model.Nome;
                usu.Telefone = model.Telefone;
                usu.Estado   = "MG";
                usu.Cidade   = "Cataguases";

                user.DadosUsuario = usu;

                IdentityUserRole role = new IdentityUserRole();
                role.RoleId = "1";
                role.UserId = user.Id;
                user.Roles.Add(role);

                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }