Esempio n. 1
0
        public async Task <Investment> UpdateInvestment(Investment obj = null)
        {
            var userName = User.Identity.Name;

            var user = await _portfolioService.GetUser(userName);

            if (user != null)
            {
                if (obj.AccountId > 0)
                {
                    var accountObj = user.Accounts.Find(x => x.AccountId == obj.AccountId);

                    if (accountObj != null)
                    {
                        Portfolio.Data.Model.Investment item = null;

                        if (obj.InvestmentId > 0)
                        {
                            item = await _portfolioService.UpdateInvestment(obj.InvestmentId, obj.Ticker, obj.Quantity);
                        }
                        else
                        {
                            item = await _portfolioService.AddInvestment(accountObj, obj.Ticker, obj.Quantity);
                        }
                        return(new Investment(item));
                    }
                }
            }

            return(null);
        }
Esempio n. 2
0
        public async Task <IActionResult> GoogleLoginCallback()
        {
            // get result from google signinscheme
            var externalAuth = await HttpContext.AuthenticateAsync("ExternalAuth");

            // google claims
            var externalClaims = externalAuth.Principal.Claims.ToList();

            var key = externalClaims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier);

            // google guid / key - subject id
            var externalKey   = externalClaims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var externalEmail = externalClaims.FirstOrDefault(x => x.Type == ClaimTypes.Email).Value;
            var externalName  = externalClaims.FirstOrDefault(x => x.Type == ClaimTypes.Name).Value;
            var externalImage = externalClaims.FirstOrDefault(x => x.Type == "image").Value;

            User user = null;

            // get uset by profile
            var profile = _portfolioContext.LoginProfiles
                          .Where(x => x.ProviderKey == externalKey && x.ProviderName == "Google")
                          .Include(x => x.User)
                          .Include(x => x.User.UserAccess).ThenInclude(y => y.Role)
                          .FirstOrDefault();

            if (profile != null)
            {
                // update profile
                profile.LoginDate = DateTime.Now;
                profile.LoginCount++;
                user = profile.User;
                _portfolioContext.SaveChanges();
            }

            if (user == null)
            {
                // get user by google id & create profile
                user = _portfolioContext.Users.Where(x => x.UserName == externalEmail)
                       .Include(x => x.UserAccess).ThenInclude(y => y.Role)
                       .FirstOrDefault();

                // create user if needed
                if (user == null)
                {
                    // hash password
                    //var passwordHasher = new PasswordHasher<string>();
                    //var _passwordHash = passwordHasher.HashPassword(externalEmail, "welcome");
                    user = new User()
                    {
                        Email = externalEmail, UserName = externalEmail, PasswordHash = null
                    };
                    _portfolioContext.Users.Add(user);
                    _portfolioContext.SaveChanges();

                    // create started account and investments
                    var account = await _dataservice.AddAccount(user, "New Account");

                    await _dataservice.AddInvestment(account, "ACRE", 15000);

                    await _dataservice.AddInvestment(account, "GGN", 50000);

                    //await _dataservice.AddInvestment(account, "GGT", 10000);
                    await _dataservice.AddInvestment(account, "NYMT", 40000);

                    await _dataservice.AddInvestment(account, "PHK", 30000);

                    await _dataservice.AddInvestment(account, "T", 6000);

                    await _dataservice.AddInvestment(account, "XOM", 4000);
                }

                if (user != null)
                {
                    profile = new LoginProfile()
                    {
                        LoginCount   = 1,
                        ProviderName = "Google",
                        ProviderKey  = externalKey,
                        UserName     = externalEmail,
                        Picture      = externalImage,
                        LoginDate    = DateTime.Now,
                        Created      = DateTime.Now,
                    };

                    user.LoginProfiles.Add(profile);
                    _portfolioContext.SaveChanges();
                }
            }

            var claims = new List <Claim>();


            foreach (var claimn in externalClaims)
            {
                if (claimn.Type != ClaimTypes.NameIdentifier && claimn.Type != ClaimTypes.Role && claimn.Type != ClaimTypes.Email && claimn.Type != ClaimTypes.Name)
                {
                    claims.Add(new Claim(claimn.Type, claimn.Value));
                }
            }

            if (user != null)
            {
                var newClaims = new List <Claim>()
                {
                    new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()),
                    new Claim(ClaimTypes.Email, user.Email),
                    new Claim(ClaimTypes.Name, user.UserName),
                    new Claim(ClaimTypes.Role, "google"),
                    new Claim(ClaimTypes.Role, "user"),
                    //new Claim(ClaimTypes.Role, user.UserId.ToString()),
                    //new Claim(ClaimTypes.NameIdentifier, user.UserId.ToString()),
                };
                foreach (var claimn in newClaims)
                {
                    claims.Add(new Claim(claimn.Type, claimn.Value));
                }
            }

            foreach (var role in user.Roles)
            {
                claims.Add(new Claim(ClaimTypes.Role, role.Name));
            }

            // create identity for cookie scheme
            var itentity  = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
            var principal = new ClaimsPrincipal(itentity);

            // delete temporary cookue used during google auth
            await HttpContext.SignOutAsync("ExternalAuth");

            // login with cookie auth
            //await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);
            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal,
                                          new AuthenticationProperties
            {
                IsPersistent = true,
                ExpiresUtc   = DateTime.UtcNow.AddMonths(1)
            }
                                          );

            return(LocalRedirect(externalAuth.Properties.Items["returnUrl"]));
        }