Example #1
0
        public ActionResult ExternalLoginCallback(string returnUrl, string token)
        {
            AuthenticationResult result = _membershipProvider.VerifyOAuthAuthentication(Url.Action("ExternalLoginCallback", new { ReturnUrl = returnUrl, Token = token }));

            if (!result.IsSuccessful)
            {
                SetErrorAlert(result.Error != null ? result.Error.Message : "An error occurred while trying to login.");
                return(RedirectToAction("ExternalLoginFailure"));
            }

            // TODO: Need to check to see if we have a user with the specified email address already.
            OAuthAccount account = result.ToOAuthAccount();

            if (_membershipProvider.OAuthLogin(account, remember: true))
            {
                if (!String.IsNullOrEmpty(token))
                {
                    AddInvitedUserToOrganization(token, _membershipProvider.GetUserByEmailAddress(account.EmailAddress() ?? account.Username));
                }

                return(RedirectToLocal(returnUrl));
            }

            // If the current user is logged in, then add the new OAuth account.
            if (User != null && User.Identity.IsAuthenticated)
            {
                var user = _membershipProvider.CreateOAuthAccount(account, new User {
                    EmailAddress = User.Identity.Name
                });                                                                                                         // TODO: @eric, why not pass in: User.UserEntity....
                if (!String.IsNullOrEmpty(token))
                {
                    AddInvitedUserToOrganization(token, user);
                }

                return(RedirectToLocal(returnUrl));
            }

            // User is new, ask for their desired membership name
            string loginData = _encoder.Protect(JsonConvert.SerializeObject(account));

            ViewBag.ProviderDisplayName = _membershipProvider.GetOAuthClientData(result.Provider).DisplayName;
            ViewBag.ReturnUrl           = returnUrl;
            ViewBag.Token = token;

            return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationModel {
                FullName = account.FullName(),
                EmailAddress = account.EmailAddress(),
                ExternalLoginData = loginData
            }));
        }