protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, UserProfileAuthorizationPolicyRequirement requirement) { var mvcContext = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext; string idParam = mvcContext.HttpContext.Request.Path.Value.Split(new char[] { '/' }).Last(); if (!context.User.HasClaim(c => c.Type == ClaimTypesCustom.UserID)) { return(Task.CompletedTask); } if (!context.User.HasClaim(c => c.Type == ClaimTypesCustom.Role)) { return(Task.CompletedTask); } if (idParam != null && ClaimHelpers.GetUserIDClaimValue((ClaimsIdentity)context.User.Identity) == Int32.Parse(idParam)) { context.Succeed(requirement); } if (context.User.Claims.Any(c => c.Type == ClaimTypesCustom.Role && c.Value == RolesEnum.Admin.ToString())) { context.Succeed(requirement); } return(Task.CompletedTask); }
public BaseController(IBaseRepository baseRepository, IHttpContextAccessor httpContextAccessor) { if (httpContextAccessor.HttpContext.User != null && httpContextAccessor.HttpContext.User.Identity.IsAuthenticated) { baseRepository.UserID = ClaimHelpers.GetUserIDClaimValue((ClaimsIdentity)httpContextAccessor.HttpContext.User.Identity); } }
public ActionResult Login(string email, string password, string returnUrl = null) { ViewData["ReturnUrl"] = returnUrl; if (!string.IsNullOrWhiteSpace(email) && !string.IsNullOrWhiteSpace(password)) { UserProfileDTO userProfile = this.userRepository.GetUserByNameAndPassword(email, password); if (userProfile != null) { var principal = new ClaimsPrincipal(ClaimHelpers.ConstructClaimsIdentity(userProfile)); HttpContext.Authentication.SignInAsync("Cookies", principal); if (returnUrl != null) { if (returnUrl != null && returnUrl.Contains("Home/") || returnUrl.Contains("Users/Edit/0")) { return(RedirectToAction("Edit", "Users", new { @id = ClaimHelpers.GetUserIDClaimValue((ClaimsIdentity)principal.Identity) })); } return(LocalRedirect(returnUrl)); } else { return(RedirectToAction("Index", "Home")); } } } return(View()); }
public static int GetUserId(this ClaimsPrincipal User) { int userId = 0; if (User != null) { try { userId = ClaimHelpers.GetUserIDClaimValue((ClaimsIdentity)User.Identity); } catch (Exception ex) { } } return(userId); }
protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, BankAccountOperationsAuthorizationPolicyRequirement requirement) { var mvcContext = context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext; string idParam = mvcContext.HttpContext.Request.Path.Value.Split(new char[] { '/' }).Last(); var id = mvcContext.ActionDescriptor.Id; BankAccountRepository bankAccountRepo = new BankAccountRepository((int)UserEnum.SystemUser); int accountOwnerUserID = bankAccountRepo.GetBankAccountOwnerId(Int32.Parse(idParam)); if (accountOwnerUserID == ClaimHelpers.GetUserIDClaimValue((ClaimsIdentity)context.User.Identity)) { context.Succeed(requirement); } if (context.User.Claims.Any(c => c.Value == requirement.RoleName)) { context.Succeed(requirement); } return(Task.CompletedTask); }