Пример #1
0
        public async Task <IActionResult> GitHubLoginCallback(string returnUrl = null, string remoteError = null)
        {
            returnUrl = returnUrl ?? this.Url.Content("~/");

            if (this.User.Identity.IsAuthenticated)
            {
                return(this.LocalRedirect(returnUrl));
            }

            if (remoteError != null)
            {
                return(this.RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            var info = await this.signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(this.RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            var result = await this.signInManager.ExternalLoginSignInAsync("GitHub", info.ProviderKey,
                                                                           isPersistent : true, bypassTwoFactor : true);

            if (result.Succeeded)
            {
                return(this.LocalRedirect(returnUrl));
            }

            this.ViewData["ReturnUrl"] = returnUrl;

            var model = new UserGitHubRegisterBindingModel();

            if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Email))
            {
                model.Email = info.Principal.FindFirstValue(ClaimTypes.Email);
            }

            if (info.Principal.HasClaim(c => c.Type == ClaimTypes.Name))
            {
                var name = info.Principal.FindFirstValue(ClaimTypes.Name);

                var split = name.Split(' ', StringSplitOptions.RemoveEmptyEntries);

                if (split.Length >= 2)
                {
                    model.FirstName = split[0];
                    model.LastName  = split[split.Length - 1];
                }
                else
                {
                    model.FirstName = name;
                }
            }

            return(this.View("GitHubRegister", model));
        }
Пример #2
0
        public async Task <IActionResult> GitHubRegister(UserGitHubRegisterBindingModel model, string returnUrl = null)
        {
            returnUrl = returnUrl ?? this.Url.Content("~/");

            if (this.User.Identity.IsAuthenticated)
            {
                return(this.LocalRedirect(returnUrl));
            }

            var info = await this.signInManager.GetExternalLoginInfoAsync();

            if (info == null)
            {
                return(this.RedirectToAction("Login", new { ReturnUrl = returnUrl }));
            }

            if (this.ModelState.IsValid)
            {
                var user = new PoolItUser
                {
                    UserName  = model.Email,
                    Email     = model.Email,
                    FirstName = model.FirstName,
                    LastName  = model.LastName
                };

                var result = await this.signInManager.UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await this.signInManager.UserManager.AddLoginAsync(user, info);

                    if (result.Succeeded)
                    {
                        this.Success(string.Format(NotificationMessages.RegistrationWelcome, model.FirstName));

                        await this.signInManager.SignInAsync(user, isPersistent : true);

                        return(this.LocalRedirect(returnUrl));
                    }
                }

                foreach (var error in result.Errors)
                {
                    this.ModelState.AddModelError(string.Empty, error.Description);
                }
            }

            this.ViewData["ReturnUrl"] = returnUrl;
            return(this.View());
        }