Exemplo n.º 1
0
        public async Task <IActionResult> getUserRolesAsync()
        {
            Console.WriteLine();
            var roles        = roleManager.Roles.ToList();
            var loggedInUser = await userManager.FindByNameAsync(User.Identity.Name.ToString());

            int i = 0;

            foreach (var ApplicationRole in roles)
            {
                //https://stackoverflow.com/questions/40151224/user-isinrole-returns-nothing-in-asp-net-core-repository-pattern-implemented
                //IsInRole checks cookie and not against database!
                var role = ApplicationRole.ToString();

                if (                /*User.IsInRole(role.ToString())*/
                    await userManager.IsInRoleAsync(loggedInUser, role)
                    )
                {
                    Console.WriteLine($"{User.Identity.Name} is in Role number {i}: {role}");
                    i++;
                }
                else
                {
                    Console.WriteLine($"{User.Identity.Name} is NOT in Role number {i}: {role}");
                    i++;
                }
            }
            return(RedirectToAction("permission"));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> OnPostAsync(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            ApplicationRole = await _roleManager.FindByIdAsync(id);

            IdentityResult roleRuslt = await _roleManager.DeleteAsync(ApplicationRole);


            // Create an auditrecord object
            var auditrecord = new AuditRecord();

            auditrecord.AuditActionType    = "Deleted a Role (Role: " + ApplicationRole.ToString() + ")";
            auditrecord.DateTimeStamp      = DateTime.Now;
            auditrecord.KeySoftwareFieldID = 0;
            // Get current logged-in user
            var userID = User.Identity.Name.ToString();

            auditrecord.Username = userID;

            _context.AuditRecords.Add(auditrecord);
            await _context.SaveChangesAsync();

            return(RedirectToPage("./Index"));
        }
        public string ProfilePath()
        {
            if (ApplicationRole == 0)
            {
                return("Patient/Profile");      //### Default to Patient
            }

            return($"{ApplicationRole.ToString()}/Profile");      //### Default to Patient
        }
        public async Task <IActionResult> OnPost(string roleName)
        {
            if (ModelState.IsValid)
            {
                ApplicationRole applicationRole = new ApplicationRole
                {
                    Name = CreateRole.Name
                };
                var roleExist = _roleManager.RoleExistsAsync(applicationRole.ToString());

                IdentityResult result = await _roleManager.CreateAsync(applicationRole);

                if (result.Succeeded)
                {
                    foreach (var SelectedClaim in ClaimView.Claims.Where(c => c.IsSelected).Select(c => new Claim(c.ClaimType, c.ClaimType)))
                    {
                        var AddClaimToRole = await _roleManager.AddClaimAsync(applicationRole, SelectedClaim);
                    }
                }
            }
            SuccessRoleCreation = "Grupa zosta³a utworzona";
            return(RedirectToPage("./Ustawienia"));
        }