private async Task <UserToken> BuildToken(UserTemp userInfo) { var user = await _userManager.FindByNameAsync(userInfo.USERID); var userClaims = await _userManager.GetClaimsAsync(user); userClaims.Add(new Claim(JwtRegisteredClaimNames.UniqueName, userInfo.USERID)); userClaims.Add(new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())); //userClaims.Add(new Claim(ClaimTypes.Role, "User")); //userClaims.Add(new Claim(ClaimTypes.Role, "Admin")); //userClaims.Add(new Claim(ClaimTypes.Role, "SuperAdmin")); var claims = new[] { new Claim(JwtRegisteredClaimNames.UniqueName, userInfo.USERID), new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString()) }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["JWT:key"])); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); // tempo de expiração do token: 1 hora var expiration = DateTime.UtcNow.AddHours(1); JwtSecurityToken token = new JwtSecurityToken( issuer: "http://localhost:45092/", audience: "http://localhost:45092/", claims: userClaims, expires: expiration, signingCredentials: creds); return(new UserToken() { Token = new JwtSecurityTokenHandler().WriteToken(token), Expiration = expiration }); }
public UserTemp SaveUserTemp(UserTemp userTemp) { UserTemp result = null; UserTemp dbEntry = context.UserTemps.Where(p => p.Email == userTemp.Email).FirstOrDefault(); if (dbEntry != null) { dbEntry.Date = DateTime.Now; dbEntry.Email = userTemp.Email; dbEntry.Nick = userTemp.Nick; dbEntry.Password = userTemp.Password; dbEntry.Token = userTemp.Token; result = dbEntry; } else { UserTemp newdbEntry = new UserTemp { Date = DateTime.Now, Email = userTemp.Email, Nick = userTemp.Nick, Password = userTemp.Password, Token = userTemp.Token }; context.UserTemps.Add(newdbEntry); result = newdbEntry; } context.SaveChanges(); return(result); }
public async Task <IActionResult> LoginUser(UserTemp user) { var userInfo = await _userManager.FindByNameAsync(user.USERID); if (user == null) { return(BadRequest("Invalid credentials")); } var result = await _signInManager.CheckPasswordSignInAsync(userInfo, user.PASSWORD, true); if (!result.Succeeded) { return(BadRequest("Invalid credentials")); } var userToken = await BuildToken(user); if (userToken.Token != null) { //Save token in session object HttpContext.Session.SetString("JWToken", userToken.Token); } return(Redirect("~/Index")); }
public IActionResult Order(int status = 0, int page = 1) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); string[] option = new String[] { "Ordering", "OrderSuccess", "All" }; if (status <= 2) { ViewBag.option = option[status]; } var context = new istoreContext(); List <Payorder> order = null; if (status != 2) { order = context.Payorder.Where(P => P.Status == status).OrderByDescending(o => o.Date).ToList(); } else { // ALL Order order = context.Payorder.OrderByDescending(o => o.Date).ToList(); } return(View(order)); }
public IActionResult Index(Account account) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); var context = new istoreContext(); var ec = new Encryption(); var item = context.Account.Where(p => p.Email == userTemp.account.Email).FirstOrDefault(); item.Password = (account.Password == "" || account.Password == null) ? item.Password : ec.EncryptionText(account.Password); item.FirstName = account.FirstName; item.LastName = account.LastName; item.Phone = account.Phone; context.Update <Account>(item); context.SaveChanges(); UpdateAppHttpContext(userTemp.account.Email); item.Password = ""; return(View(item)); }
public IActionResult Login(Account account) { account.Password = new Encryption().EncryptionText(account.Password); UserTemp temp = new UserTemp(); var context = new istoreContext(); temp.account = context.Account.Where(a => a.Email == account.Email && a.Password == account.Password).FirstOrDefault(); if (temp.account == null) { return(RedirectToAction("Index", "Home")); } temp.Carts = context.Carts.Where(a => a.AccountEmail == account.Email).ToList(); temp.account.Password = ""; string json = JsonConvert.SerializeObject(temp, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); AppHttpContext.Current.Session.SetString("Tempdata", json); return(RedirectToAction("Index", "Home")); }
public User TransferUserTemp(string token, int source = 0) { User user = null; UserTemp userTemp = context.UserTemps.Where(p => p.Token == token).FirstOrDefault(); if (userTemp != null) { user = context.Users.FirstOrDefault(p => p.Email == userTemp.Email); if (user != null) { user.Password = userTemp.Password; user.Source = 0; this.Save(user); } else { user = new User { Date = DateTime.Now, Email = userTemp.Email, Nick = userTemp.Nick, Password = userTemp.Password, Status = 1, Source = source }; this.Save(user); context.UserTemps.Remove(userTemp); context.SaveChanges(); } } return(user); }
public IActionResult Logout() { UserTemp temp = new UserTemp(); string json = JsonConvert.SerializeObject(temp); AppHttpContext.Current.Session.SetString("Tempdata", json); return(RedirectToAction("Index", "Home")); }
public User New(UserTemp userTemp) { User user = new User { Username = userTemp.username, IsAdmin = userTemp.isAdmin }; return(userRepo.Add(user, userTemp.password)); }
public IActionResult Details(int Id) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); List <Carts> CartsList = new List <Carts>(); using (var context = new istoreContext()) { var pay = context.Payorder.Where(P => P.PayorderId == Id).ToList(); var paydetail = context.Orderdetail.Join(pay, p => p.PayorderId, o => o.PayorderId, (o, p) => new { orderId = o.Id, payorder_id = p.PayorderId, product_id = o.ProductId, product_price = o.ProductPrice, quantity = o.Quantity, TotalPrice = p.TotalPrice, date = p.Date }); var pay_detail_product = context.Products.Join(paydetail, p => p.Id, pd => pd.product_id, (p, pd) => new { pd.orderId, pd.payorder_id, pd.product_price, pd.quantity, pd.TotalPrice, pd.date, product = p }); foreach (var i in pay_detail_product) { i.product.Price = i.product_price; CartsList.Add( new Carts { Id = i.orderId, AccountEmail = userTemp.account.Email, Product = i.product, Quantity = i.quantity ?? 0, AccountEmailNavigation = userTemp.account }); } } return(View(CartsList)); }
public UserTemp UserTempByToken(string token) { if (!string.IsNullOrEmpty(token)) { UserTemp dbEntry = context.UserTemps.Where(p => p.Token == token).FirstOrDefault(); return(dbEntry); } else { return(null); } }
public string GetUniqueToken(string token, IUserRepository repo) { UserTemp userTemp = repo.UserTempByToken(token); if (userTemp != null) { return(this.GetUniqueToken(textBuilder.GetRandomString(), repo)); } else { return(token); } }
//Using hard coded collection list as Data Store for demo. In reality, User data comes from Database or some other Data Source - JRozario private IEnumerable <Claim> GetUserClaims(UserTemp user) { IEnumerable <Claim> claims = new Claim[] { new Claim(ClaimTypes.Name, user.FIRST_NAME + " " + user.LAST_NAME), new Claim("USERID", user.USERID), new Claim("EMAILID", user.EMAILID), new Claim("PHONE", user.PHONE), new Claim("ACCESS_LEVEL", user.ACCESS_LEVEL.ToUpper()), new Claim("READ_ONLY", user.READ_ONLY.ToUpper()) }; return(claims); }
private void UpdateAppHttpContext(string email) { var context = new istoreContext(); UserTemp userTemp = new UserTemp(); userTemp.account = context.Account.Where(a => a.Email == email).FirstOrDefault(); userTemp.Carts = context.Carts.Where(c => c.AccountEmail == email).ToList(); userTemp.account.Password = ""; string json = JsonConvert.SerializeObject(userTemp, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); AppHttpContext.Current.Session.SetString("Tempdata", json); }
public IActionResult Index() { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); var context = new istoreContext(); var item = context.Account.Where(p => p.Email == userTemp.account.Email).FirstOrDefault(); item.Password = ""; return(View(item)); }
public HttpResponseMessage EmailReport(UserTemp user) { try { /* 30/12 no final nadad disso era necessario */ //var retortSerial = JsonConvert.SerializeObject(report, Formatting.Indented, new JsonSerializerSettings // { // ReferenceLoopHandling = ReferenceLoopHandling.Serialize, // PreserveReferencesHandling = PreserveReferencesHandling.Objects // }); //var response = JsonConvert.DeserializeObject<Report>(retortSerial); //return Request.CreateResponse(HttpStatusCode.OK, response); return(Request.CreateResponse(HttpStatusCode.OK, RepositoryFactory.GetRepository <IReport>().EmailReport(user.UserID, user.UserEmail))); } catch (Exception ex) { return(Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message)); } }
public IActionResult RemoveItem(int productid) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); var context = new istoreContext(); Carts carts = context .Carts.Where(c => c.AccountEmail == userTemp.account.Email && c.ProductId == productid) .FirstOrDefault(); context.Remove <Carts>(carts); context.SaveChanges(); UpdateAppHttpContext(userTemp.account.Email); return(RedirectToAction("Index")); }
public IActionResult Checkout() { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); List <Carts> ItemCart = new List <Carts>(); using (var context = new istoreContext()) { IEnumerable <Carts> carts = context.Carts.Where(c => c.AccountEmail == userTemp.account.Email); var Datalist = carts.Join(context.Products, ca => ca.ProductId, pro => pro.Id, (ca, pro) => new { Email = ca.AccountEmail, CartsID = ca.Id, Product = pro, Quantity = ca.Quantity }); foreach (var i in Datalist) { ItemCart.Add(new Carts { Id = i.CartsID, ProductId = i.Product.Id, Quantity = i.Quantity, AccountEmail = i.Email, Product = i.Product, }); } } return(View(ItemCart)); }
public IActionResult AddItem(int productid, int quantity) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); var context = new istoreContext(); Carts carts = context .Carts.Where(c => c.AccountEmail == userTemp.account.Email && c.ProductId == productid) .FirstOrDefault(); if (carts != null) { carts.Quantity += quantity; context.Update <Carts>(carts); context.SaveChanges(); } else { Carts c = new Carts { AccountEmail = userTemp.account.Email, ProductId = productid, Quantity = quantity }; context.Carts.Add(c); context.SaveChanges(); } UpdateAppHttpContext(userTemp.account.Email); return(RedirectToAction("Index")); }
public string CheckProduct() { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; List <Products> product = new List <Products>(); if (json == null) { return(JsonConvert.SerializeObject(product, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects })); } userTemp = new UserTemp().Mapjson(json); List <Carts> ItemCart = new List <Carts>(); using (var context = new istoreContext()) { IEnumerable <Carts> carts = context.Carts.Where(c => c.AccountEmail == userTemp.account.Email); var Datalist = carts.Join(context.Products, ca => ca.ProductId, pro => pro.Id, (ca, pro) => new { Email = ca.AccountEmail, CartsID = ca.Id, Product = pro, Quantity = ca.Quantity }); foreach (var i in Datalist) { ItemCart.Add(new Carts { Id = i.CartsID, ProductId = i.Product.Id, Quantity = i.Quantity, AccountEmail = i.Email, Product = i.Product, }); } } foreach (var item in ItemCart) { if (item.Quantity > item.Product.Amount) { item.Product.Image = new byte[0]; product.Add(item.Product); } } string Productjson = JsonConvert.SerializeObject(product, Formatting.Indented, new JsonSerializerSettings { PreserveReferencesHandling = PreserveReferencesHandling.Objects }); return(Productjson); }
public ActionResult <User> Post([FromBody] UserTemp value) { return(_userService.New(value)); }
public IActionResult Confirm(Payorder payorder) { var json = AppHttpContext.Tempdate("Tempdata"); UserTemp userTemp = null; if (json == null) { return(RedirectToAction("Index", "Home")); } userTemp = new UserTemp().Mapjson(json); if (userTemp.CartsCount == 0) { return(RedirectToAction("Index", "Home")); } List <Orderdetail> order = new List <Orderdetail>(); List <Products> products = new List <Products>(); // getdata using (var context = new istoreContext()) { IEnumerable <Carts> carts = context.Carts.Where(c => c.AccountEmail == userTemp.account.Email); var Datalist = carts.Join(context.Products, ca => ca.ProductId, pro => pro.Id, (ca, pro) => new { Email = ca.AccountEmail, CartsID = ca.Id, Product = pro, Quantity = ca.Quantity }); payorder.TotalPrice = 0; payorder.Email = userTemp.account.Email; payorder.Date = DateTime.Now; foreach (var i in Datalist) { order.Add(new Orderdetail { Quantity = i.Quantity, ProductId = i.Product.Id, ProductPrice = i.Product.Price }); payorder.TotalPrice += (i.Quantity * i.Product.Price); if (i.Product.Amount - i.Quantity < 0) { RedirectToAction("Index", "Cart"); } i.Product.Amount -= i.Quantity; products.Add(i.Product); } } //save data using (var context = new istoreContext()) { context.Payorder.Add(payorder); context.SaveChanges(); foreach (var item in order) { item.PayorderId = payorder.PayorderId; } context.AddRange(order); context.UpdateRange(products); var cartsList = context.Carts.Where(c => c.AccountEmail == "*****@*****.**").ToList(); context.RemoveRange(cartsList); context.SaveChanges(); } UpdateAppHttpContext(userTemp.account.Email); return(View()); }
public static void SaveJson() { Console.WriteLine("test jsonValue Attributes"); Console.WriteLine("Mssql"); using (var rep = new Repository(DataBaseTypes.Mssql)) { var user = rep.Get <User>().LoadChildren().ExecuteFirstOrDefault(); var jsonUser = new UserTemp() { User = user, UserXml = user, }; rep.Save(jsonUser); var userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); jsonUser.User.UserName = "******"; rep.Save(jsonUser); rep.SaveChanges(); userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); Console.WriteLine((jsonUser.User.UserName == "test" ? "Success" : "Failed")); } Console.WriteLine("PostgreSql"); using (var rep = new Repository(DataBaseTypes.PostgreSql)) { var user = rep.Get <User>().LoadChildren().ExecuteFirstOrDefault(); var jsonUser = new UserTemp() { User = user, UserXml = user, }; rep.Save(jsonUser); var userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); jsonUser.User.UserName = "******"; rep.Save(jsonUser); rep.SaveChanges(); userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); Console.WriteLine((jsonUser.User.UserName == "test" ? "Success" : "Failed")); } Console.WriteLine("Sqlite"); using (var rep = new Repository(DataBaseTypes.Sqllight)) { var user = rep.Get <User>().LoadChildren().ExecuteFirstOrDefault(); var jsonUser = new UserTemp() { User = user, UserXml = user, }; rep.Save(jsonUser); var userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); jsonUser.User.UserName = "******"; rep.Save(jsonUser); rep.SaveChanges(); userJson = rep.Get <UserTemp>().ExecuteFirstOrDefault(); Console.WriteLine((jsonUser.User.UserName == "test" ? "Success" : "Failed")); } }
public string Post(UserTemp m) //post方式请求 { return("value"); }