示例#1
0
        public async Task <ActionResult> ExternalLoginConfirmation(string returnUrl)
        {
            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 firstName = info.ExternalIdentity.Claims.FirstOrDefault(u => u.Type == ClaimTypes.GivenName).Value;
                string lastName  = info.ExternalIdentity.Claims.FirstOrDefault(u => u.Type == ClaimTypes.Surname).Value;
                var    user      = new User {
                    UserName = info.Email, Email = info.Email, FirstName = firstName, LastName = lastName
                };

                var createUserResult = await _userService.CreateAsync(user);

                if (createUserResult.Succeeded)
                {
                    // Add login.
                    var addUserResult = await _userService.AddLoginAsync(user.Id, info.Login);

                    // Add user to the 'User' role.
                    _userService.AddToRole(user.Id, UserRoles.User);

                    if (addUserResult.Succeeded)
                    {
                        await _signInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
            }
            return(RedirectToLocal(returnUrl));
        }