public async Task <ActionResult <Admins> > Edit(string id, Admins value) { // var product = new Product(value.Id); value.Id = ObjectId.Parse(id); Admins admin = await _Admins.GetById(id); _Admins.Update(value, id); Login Login = new Login(); Login = await Auth.GetUser(admin.Email); if (admin.Email != value.Email) { Random generator = new Random(); string password = generator.Next(0, 999999).ToString("D6"); await sender.SendEmailAsync(value.Email, "Your account is successfully created", "Please use this code <b>" + password + "</b> for login "); Login.Email = value.Email; Login.RandomPass = password; Login.Role = value.Role; Login.PasswordHash = null; Login.PasswordSalt = null; Login.sentdate = DateTime.Now.ToShortDateString(); } else { Login.Role = value.Role; } _login.Update(Login, Convert.ToString(Login.Id)); await _uow.Commit(); return(RedirectToAction("Index")); }
public async Task <ActionResult> Login(LoginViewModel l, string ReturnUrl) { ViewBag.ReturnUrl = ReturnUrl; if (await auth.IsUserExists(l.Email)) { var login = auth.Login(l.Email, l.Password); Login user = await auth.GetUser(l.Email); string pass = user.RandomPass; if (login.Result != null) { var Admin = login; if (Url.IsLocalUrl(ReturnUrl)) { //var objAdmin = context.login.FirstOrDefault(a => (a.Email == l.Email)); //FormsAuthentication.SetAuthCookie(l.Email, false); HttpContext.Session.SetString("id", Convert.ToString(user.Id)); HttpContext.Session.SetString("userEmail", user.Email); //Session.Add("category", Admin.Role); return(Redirect(ReturnUrl)); } else { const string Issuer = "my issuer"; var claims = new List <Claim>(); claims.Add(new Claim(ClaimTypes.Name, l.Email, ClaimValueTypes.String, Issuer)); // claims.Add(new Claim(Constants., user., ClaimValueTypes.String, Constants.Issuer)); // claims.Add(new Claim(Constants.CompanyClaimType, user.Company, ClaimValueTypes.String, Constants.Issuer)); claims.Add(new Claim(ClaimTypes.Role, user.Role, ClaimValueTypes.String, Issuer)); var userIdentity = new ClaimsIdentity("Debugsoft"); userIdentity.AddClaims(claims); var userPrincipal = new ClaimsPrincipal(userIdentity); await HttpContext.SignInAsync( "AdminCookie", userPrincipal, new AuthenticationProperties { ExpiresUtc = DateTime.UtcNow.AddMinutes(100), IsPersistent = false, AllowRefresh = false }); // Set current principal Thread.CurrentPrincipal = userPrincipal; var identity = (ClaimsPrincipal)Thread.CurrentPrincipal; var a = Thread.CurrentPrincipal.Identity.Name; // Get the claims values var name = identity.Claims.Where(c => c.Type == ClaimTypes.Name) .Select(c => c.Value).SingleOrDefault(); if (user.Role == "SuperAdmin") { return(RedirectToAction("Index", "Post")); } else { return(RedirectToAction("Index", "Post")); } } } else if (l.Password == pass) { TempData["message"] = l.Email; return(RedirectToAction("NewPassword")); } else { ModelState.AddModelError("", "Invalid Password"); } } ModelState.AddModelError("", "Invalid User"); return(View()); }