Ejemplo n.º 1
0
 public ActionResult UpdatePhoto(HttpPostedFileBase PhotoFile)
 {
     if (PhotoFile != null && PhotoFile.ContentLength > 0)
     {
         var file = AzureFileHelper.AddPhoto(User.Identity.GetUserId(), PhotoFile);
         var user = BLLUsers.GetUserById(User.Identity.GetUserId());
         user.PhotoPath = file.Uri.AbsoluteUri;
         BLLUsers.UpdateUser(user);
     }
     return(RedirectToAction("Profil"));
 }
Ejemplo n.º 2
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            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 (!String.IsNullOrWhiteSpace(returnUrl))
                {
                    return(Redirect(returnUrl));
                }
                return(RedirectToAction("Profil", "Account"));

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

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

            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;

                ApplicationUser au = null;

                if (loginInfo.Login.LoginProvider.ToLower() == "facebook")
                {
                    au = new ApplicationUser
                    {
                        UserName   = loginInfo.Email,
                        Email      = loginInfo.Email,
                        CreateDate = DateTime.Now,
                        FirstName  = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "first_name").Value,
                        LastName   = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "last_name").Value,
                    };
                }
                else
                {
                    au = new ApplicationUser
                    {
                        UserName   = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").Value,
                        Email      = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress").Value,
                        CreateDate = DateTime.Now,
                        FirstName  = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname").Value,
                        LastName   = loginInfo.ExternalIdentity.Claims.First(x => x.Type == "http://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname").Value,
                    };
                }

                var existingUser = BLLUsers.GetUserByEmail(loginInfo.Email);
                if (existingUser != null)
                {
                    BLLUsers.AssociateUserAccounts(loginInfo.Login.LoginProvider, loginInfo.Login.ProviderKey, existingUser.Id);

                    if (loginInfo.Login.LoginProvider == "facebook")
                    {
                        existingUser.PhotoPath = String.Format("http://graph.facebook.com/{0}/picture", loginInfo.ExternalIdentity.Claims.First(x => x.Type == "id").Value);
                        BLLUsers.UpdateUser(existingUser);
                    }
                    au.Id = existingUser.Id;
                    await SignInManager.SignInAsync(au, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Profil", "Account"));
                }


                var res = await UserManager.CreateAsync(au);

                if (res.Succeeded)
                {
                    res = await UserManager.AddLoginAsync(au.Id, loginInfo.Login);

                    var u = BLLUsers.GetUserById(au.Id);
                    if (loginInfo.Login.LoginProvider == "facebook")
                    {
                        u.PhotoPath = String.Format("http://graph.facebook.com/{0}/picture", loginInfo.ExternalIdentity.Claims.First(x => x.Type == "id").Value);
                        BLLUsers.UpdateUser(u);
                    }
                    if (res.Succeeded)
                    {
                        await SignInManager.SignInAsync(au, isPersistent : false, rememberBrowser : false);

                        return(RedirectToAction("Profil", "Account"));
                    }
                    else
                    {
                        return(View("ExternalLoginFailure"));
                    }
                }
                else
                {
                    return(View("ExternalLoginFailure"));
                }
            }
        }