Ejemplo n.º 1
0
        public async Task <IActionResult> OnPostEditAsync(int?id)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }
            var loginuser         = _context.Users.Where(a => a.UserName == User.Identity.Name).FirstOrDefault();
            var loginuserrole     = _context.UserRoles.Where(a => a.UserId == loginuser.Id).FirstOrDefault();
            var loginuserroletext = _context.Roles.Where(a => a.Id == loginuserrole.RoleId).FirstOrDefault();

            if (loginuserroletext.ToString() == RoleEnum.Admin.ToString())
            {
                journal.StatusIdByRole = 3;
            }
            if (journal.ConflictStatus == true)
            {
                journal.StatusIdByRole = 1;
            }
            else if (loginuserroletext.ToString() != RoleEnum.Admin.ToString())
            {
                journal.StatusIdByRole = 2;
            }
            journal.JournalDate            = DateTime.Now.ToUniversalTime();
            journal.createdBy              = User.Identity.Name;
            _context.Attach(journal).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ConflictExists(journal.Id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(RedirectToPage("/Conflicts/Details", new { id = ConId }));
        }
        public async Task <IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            var users = new IdentityUser {
                UserName = AspNetUsers.UserName, Email = AspNetUsers.UserName, NormalizedEmail = AspNetUsers.UserName.ToUpper()
            };
            var result = await _userManager.CreateAsync(users, AspNetUsers.PasswordHash);

            if (result.Succeeded)
            {
                var user = _context.Users.Where(a => a.UserName == AspNetUsers.UserName).FirstOrDefault();
                var Role = _context.Roles.Where(r => r.Name == RoleEnum.User.ToString()).FirstOrDefault();

                var role = new IdentityUserRole <string>
                {
                    UserId = user.Id,
                    RoleId = Role.Id
                };
                var userexist = _context.UserRoles.Where(a => a.UserId == user.Id).ToList();
                if (userexist.Count == 0)
                {
                    _context.UserRoles.Add(role);
                    await _context.SaveChangesAsync();
                }

                await _signManager.SignInAsync(users, false);

                return(RedirectToPage("/Index"));
            }
            else
            {
                foreach (var error in result.Errors)
                {
                    ModelState.AddModelError("", error.Description);
                }
            }
            return(Page());
        }