Esempio n. 1
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            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 AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                string name = info.ExternalIdentity.Claims.FirstOrDefault(elem => elem.Type == ClaimTypes.Name).Value;
                // var accessToken = info.ExternalIdentity.Claims.FirstOrDefault(elem => elem.Type == "AccessToken").Value;
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email, Name = name
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    //var profileImageLink = GetProfileImageLink(accessToken,info.Login.ProviderKey);
                    Services.Services.UserService service = Services.Services.UserService.GetInstance();
                    var id = user.Id;
                    OperationResult <string> op = await service.PostUserAsync(new CreateUser
                    {
                        name         = name,
                        email        = model.Email,
                        registerDate = DateTime.Now,
                        id           = user.Id
                    });

                    if (!op.Success)
                    {
                        await UserManager.DeleteAsync(user);

                        return(RedirectToAction("Register", "Account", new { id = user.Id, message = op.Message }));
                    }

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

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

                        return(RedirectToAction("Index", "OAuth"));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }
Esempio n. 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email,
                    Email    = model.Email,
                    Name     = model.Name + " " + model.LastName,
                    //Photo = "https://s3-eu-west-1.amazonaws.com/eventcommit/Default/images.jpg"
                };

                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    Services.Services.UserService service = Services.Services.UserService.GetInstance();
                    var id = user.Id;
                    OperationResult <string> op = await service.PostUserAsync(new CreateUser {
                        name           = model.Name,
                        email          = model.Email,
                        registerDate   = DateTime.Now,
                        id             = user.Id,
                        lastName       = model.LastName,
                        profilePicture = model.Picture != null?model.Picture.InputStream:null
                    });

                    if (op.Success)
                    {
                        user.Photo = op.Result;
                        var update = await UserManager.UpdateAsync(user);

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

                            return(RedirectToAction("Index", "OAuth"));
                        }
                        await service.DeleteUser(new CreateUser { id = id });
                    }
                    await UserManager.DeleteAsync(user);

                    return(RedirectToAction("Register", "Account", new { id = user.Id, message = op.Message }));
                    // 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.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    // return RedirectToAction("Index", "Home");
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Esempio n. 3
0
        private async Task <ActionResult> ExternalLogin()
        {
            var info = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            var accessToken = info.ExternalIdentity.Claims.FirstOrDefault(elem => elem.Type == "AccessToken").Value;
            var resources   = GetResources(accessToken, info.Login.LoginProvider);

            var user = new ApplicationUser {
                UserName = resources.email, Email = resources.email, Name = resources.name, Photo = resources.picture
            };
            var result = await UserManager.CreateAsync(user);

            if (result.Succeeded)
            {
                Services.Services.UserService service = Services.Services.UserService.GetInstance();
                var id = user.Id;
                OperationResult <string> op = await service.PostUserAsync(new CreateUser
                {
                    name         = resources.first_name,
                    lastName     = resources.last_name,
                    email        = resources.email,
                    registerDate = DateTime.Now,
                    id           = user.Id,
                    picture      = resources.picture,
                    facebook     = info.Login.LoginProvider == "Facebook"? resources.link:null,
                    googleplus   = info.Login.LoginProvider == "Google"?resources.link:null,
                    local        = resources.local
                });

                if (!op.Success)
                {
                    await UserManager.DeleteAsync(user);

                    return(RedirectToAction("Register", "Account", new { id = user.Id, message = op.Message }));
                }

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

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

                    return(RedirectToAction("Index", "OAuth"));
                }
            }
            AddErrors(result);
            return(RedirectToAction("Index", "Home"));
        }