Esempio n. 1
0
        public ErrorCollection AddLogin(string userId, string loginProvider, string providerKey)
        {
            var rv = new ErrorCollection();

            AddFromIdentityResult(applicationUserManager.AddLogin(userId, new UserLoginInfo(loginProvider, providerKey)), rv);

            return(rv);
        }
        private void CreateAndLoginUser()
        {
            if (!IsValid)
            {
                return;
            }
            var manager = new ApplicationUserManager();
            var user    = new ApplicationUser()
            {
                UserName = userName.Text
            };
            IdentityResult result = manager.Create(user);

            if (result.Succeeded)
            {
                var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    Response.Redirect("~/Account/Login");
                    return;
                }
                result = manager.AddLogin(user.Id, loginInfo.Login);
                if (result.Succeeded)
                {
                    IdentityHelper.SignIn(manager, user, isPersistent: false);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    return;
                }
            }
            AddErrors(result);
        }
Esempio n. 3
0
        public StatusAccountViewModel CreateLogin(string email, string name, string surName)
        {
            var info = authenticationManager.GetExternalLoginInfo();
            var user = new AppUser
            {
                UserName = email,
                Email    = email
            };

            var result = userManager.Create(user);

            if (result.Succeeded)
            {
                result = userManager.AddLogin(user.Id, info.Login);

                if (result.Succeeded)
                {
                    UserProfile userProfile = new UserProfile();
                    userProfile.Name    = name;
                    userProfile.SurName = surName;
                    userProfile.Id      = user.Id;
                    userProfileRepository.Create(userProfile);
                    userProfileRepository.SaveChanges();
                    userManager.AddToRole(user.Id, "User");
                    signInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
                    return(StatusAccountViewModel.Success);
                }
            }

            return(StatusAccountViewModel.Error);
        }
Esempio n. 4
0
        public ActionResult ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Informationen zum Benutzer aus dem externen Anmeldeanbieter abrufen
                var info = AuthenticationManager.GetExternalLoginInfo();
                if (info == null)
                {
                    return(View("~/Areas/user/Views/Account/ExternalLoginFailure.cshtml"));
                }
                var user = new User {
                    UserName = model.Email, Email = model.Email
                };
                var result = UserManager.Create(user);
                if (result.Succeeded)
                {
                    result = UserManager.AddLogin(user.Id, info.Login);
                    if (result.Succeeded)
                    {
                        SignInManager.SignIn(user, isPersistent: false, rememberBrowser: false);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View("~/Areas/user/Views/Account/ExternalLoginConfirmation.cshtml", model));
        }
Esempio n. 5
0
        //
        // GET: /Account/LinkLoginCallback
        public ActionResult LinkLoginCallback()
        {
            var loginInfo = AuthenticationManager.GetExternalLoginInfo(XsrfKey, User.Identity.GetUserId());

            if (loginInfo == null)
            {
                return(RedirectToAction("Manage", new { Message = ManageMessageId.Error }));
            }
            IdentityResult result = UserManager.AddLogin(User.Identity.GetUserId(), loginInfo.Login);

            if (result.Succeeded)
            {
                return(RedirectToAction("Manage"));
            }
            return(RedirectToAction("Manage", new { Message = ManageMessageId.Error }));
        }
Esempio n. 6
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            ApplicationUser user      = null;
            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:
                if (returnUrl != null && Hashtable[returnUrl] != null)
                {
                    var state        = returnUrl;
                    var redirect_uri = Hashtable[state];
                    user = await UserManager.FindByEmailAsync(loginInfo.Email);

                    Hashtable[user.Id] = loginInfo;
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                    return(Redirect(string.Format("{0}?state={1}&code={2}", redirect_uri, state, user.Id)));
                }
                else
                {
                    return(RedirectToLocal(returnUrl));
                }

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

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

            case SignInStatus.Failure:
            default:
                user = new ApplicationUser {
                    UserName = loginInfo.Email, Email = loginInfo.Email
                };
                if (UserManager.Create(user).Succeeded)
                {
                    if (UserManager.AddLogin(user.Id, loginInfo.Login).Succeeded)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        goto case SignInStatus.Success;
                    }
                }
                // If the user does not have an account, then prompt the user to create an account
                //ViewBag.ReturnUrl = returnUrl;
                //ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    Email = loginInfo.Email
                }));
            }
        }
Esempio n. 7
0
        public static bool LinkUserWithExternalLogin()
        {
            IAuthenticationManager authenticationManager = HttpContext.Current.GetOwinContext().Authentication;
            ApplicationUserManager userManager           = GetUserManager();

            var loginInfo = authenticationManager.GetExternalLoginInfo();
            var result    = userManager.AddLogin(GetCurrentUserId(), loginInfo.Login);

            return(result.Succeeded);
        }
Esempio n. 8
0
 protected void Page_Load()
 {
     this.ProviderName = IdentityHelper.GetProviderNameFromRequest(this.Request);
     if (string.IsNullOrEmpty(this.ProviderName))
     {
         this.RedirectOnFail();
     }
     else
     {
         if (this.IsPostBack)
         {
             return;
         }
         ApplicationUserManager   userManager        = this.Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
         ApplicationSignInManager manager            = this.Context.GetOwinContext().Get <ApplicationSignInManager>();
         ExternalLoginInfo        externalLoginInfo1 = this.Context.GetOwinContext().Authentication.GetExternalLoginInfo();
         if (externalLoginInfo1 == null)
         {
             this.RedirectOnFail();
         }
         else
         {
             ApplicationUser user = userManager.Find <ApplicationUser, string>(externalLoginInfo1.Login);
             if (user != null)
             {
                 manager.SignIn <ApplicationUser, string>(user, false, false);
                 IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
             }
             else if (this.User.Identity.IsAuthenticated)
             {
                 ExternalLoginInfo externalLoginInfo2 = this.Context.GetOwinContext().Authentication.GetExternalLoginInfo("XsrfId", this.User.Identity.GetUserId());
                 if (externalLoginInfo2 == null)
                 {
                     this.RedirectOnFail();
                 }
                 else
                 {
                     IdentityResult result = userManager.AddLogin <ApplicationUser, string>(this.User.Identity.GetUserId(), externalLoginInfo2.Login);
                     if (result.Succeeded)
                     {
                         IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
                     }
                     else
                     {
                         this.AddErrors(result);
                     }
                 }
             }
             else
             {
                 this.email.Text = externalLoginInfo1.Email;
             }
         }
     }
 }
        public ActionResult LinkLoginCallback()
        {
            const string XsrfKey   = "XsrfId";
            var          loginInfo = AuthenticationManager.GetExternalLoginInfo(XsrfKey, User.Identity.GetUserId());

            if (loginInfo == null)
            {
                return(RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error }));
            }
            var result = UserManager.AddLogin(User.Identity.GetUserId(), loginInfo.Login);

            return(result.Succeeded ? RedirectToAction("ManageLogins") : RedirectToAction("ManageLogins", new { Message = ManageMessageId.Error }));
        }
        protected void Page_Load()
        {
            // Process the result from an auth provider in the request
            ProviderName = IdentityHelper.GetProviderNameFromRequest(Request);
            if (String.IsNullOrEmpty(ProviderName))
            {
                Response.Redirect("~/Account/Login");
            }
            if (!IsPostBack)
            {
                var manager   = new ApplicationUserManager();
                var loginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (loginInfo == null)
                {
                    Response.Redirect("~/Account/Login");
                }
                var user = manager.Find(loginInfo.Login);
                if (user != null)
                {
                    IdentityHelper.SignIn(manager, user, isPersistent: false);
                    IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                }
                else if (User.Identity.IsAuthenticated)
                {
                    // Apply Xsrf check when linking
                    var verifiedloginInfo = Context.GetOwinContext().Authentication.GetExternalLoginInfo(IdentityHelper.XsrfKey, User.Identity.GetUserId());
                    if (verifiedloginInfo == null)
                    {
                        Response.Redirect("~/Account/Login");
                    }

                    var result = manager.AddLogin(User.Identity.GetUserId(), verifiedloginInfo.Login);
                    if (result.Succeeded)
                    {
                        IdentityHelper.RedirectToReturnUrl(Request.QueryString["ReturnUrl"], Response);
                    }
                    else
                    {
                        AddErrors(result);
                        return;
                    }
                }
                else
                {
                    userName.Text = loginInfo.DefaultUserName;
                }
            }
        }
Esempio n. 11
0
        private void CreateAndLoginUser()
        {
            if (!this.IsValid)
            {
                return;
            }
            ApplicationUserManager   userManager1    = this.Context.GetOwinContext().GetUserManager <ApplicationUserManager>();
            ApplicationSignInManager userManager2    = this.Context.GetOwinContext().GetUserManager <ApplicationSignInManager>();
            ApplicationUser          applicationUser = new ApplicationUser();
            string text1 = this.email.Text;

            applicationUser.UserName = text1;
            string text2 = this.email.Text;

            applicationUser.Email = text2;
            ApplicationUser user   = applicationUser;
            IdentityResult  result = userManager1.Create <ApplicationUser, string>(user);

            if (result.Succeeded)
            {
                ExternalLoginInfo externalLoginInfo = this.Context.GetOwinContext().Authentication.GetExternalLoginInfo();
                if (externalLoginInfo == null)
                {
                    this.RedirectOnFail();
                    return;
                }
                result = userManager1.AddLogin <ApplicationUser, string>(user.Id, externalLoginInfo.Login);
                if (result.Succeeded)
                {
                    userManager2.SignIn <ApplicationUser, string>(user, false, false);
                    IdentityHelper.RedirectToReturnUrl(this.Request.QueryString["ReturnUrl"], this.Response);
                    return;
                }
            }
            this.AddErrors(result);
        }
        GetExternalLoginHandler(ModelMethodContext context)
        {
            var model = new ExternalLoginHandlerViewModel
            {
                ProviderName = StateContext.Data[ProviderNameKey] as string
            };

            // Check that a Provider is specified
            if (String.IsNullOrWhiteSpace(model.ProviderName))
            {
                ExternalLoginFailRedirect();
                return(null);
            }

            var loginInfo = _authenticationManager.GetExternalLoginInfo();

            if (loginInfo == null)
            {
                ExternalLoginFailRedirect();
                return(null);
            }

            var signInStatus = ExternalSignIn(loginInfo, false);

            switch (signInStatus)
            {
            case SignInStatus.Success:
                RedirectToLocal();
                return(null);

            case SignInStatus.NotVerified:
                var user = _userManager.Find(loginInfo.Login);
                SendEmailVerificationCode(user, reminder: true);
                return(null);

            case SignInStatus.LockedOut:
                StateController.Navigate("AccountLocked");
                return(null);

            case SignInStatus.RequiresTwoFactorAuthentication:
                StateController.Navigate(
                    "SendTwoFactorCode", new NavigationData
                {
                    { ReturnUrlKey, StateContext.Data[ReturnUrlKey] }
                });
                return(null);
            }

            if (_context.User.Identity.IsAuthenticated)
            {
                // Apply Xsrf check when linking
                var userId            = _context.User.Identity.GetUserId();
                var verifiedloginInfo = _authenticationManager
                                        .GetExternalLoginInfo(XsrfKey, userId);

                if (verifiedloginInfo == null)
                {
                    ExternalLoginFailRedirect();
                    return(null);
                }

                var result = _userManager.AddLogin(userId, verifiedloginInfo.Login);
                if (result.Succeeded)
                {
                    RedirectToLocal();
                    return(null);
                }

                AddErrors(result, context);
            }
            else
            {
                // It's a new account, so get additional local details
                model.UserName = loginInfo.DefaultUserName;
                model.Email    = loginInfo.Email;
            }

            return(model);
        }