Quantcast
Channel: .NET Framework Class Libraries forum
Viewing all articles
Browse latest Browse all 8156

Get Roles and Permission using Claims from SQL Server database

$
0
0

I have Implemented a mixed mode windows and custom STS claims authentication in an MVC 5 application. From SQL Server database, I can get a login user windows Username and check their Roles and permissions. My STS login works but I don't know how to get the Identity from the claims and check it for their Roles and Permission in the SQL Server database same as the windows account. I am using email claimtype and have a column in the SQL server database to store it. Below find my code to get the Roles and Permission when it a windows account and the Authorization module. Any help will be appreciated.

 private void GetUserRolesPermissions()
        {
            using (var _data = new CD_Model())
            {
                _data.Configuration.LazyLoadingEnabled = false;
                var _user = _data.USERS.FirstOrDefault(u => u.Username == Username);
                _data.Entry(_user).Collection(u => u.ROLES).Load();
                if (_user == null) return;
                User_Id = _user.User_Id;
                foreach (var _role in _user.ROLES)
                {
                    var _userRole = new UserRole
                    {
                        Role_Id = _role.Role_Id,
                        RoleName = _role.RoleName,
                        RoleDescription = _role.RoleDescription
                    };
                    foreach (var _permission in _role.PERMISSIONS)
                    {
                        _userRole.Permissions.Add(new PERMISSION
                        {
                            Permission_Id = _permission.Permission_Id,
                            PermissionDescription = _permission.PermissionDescription,
                            ROLES = _permission.ROLES
                        });
                    }
	*********************************************************************************************************
      protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            return httpContext.User.Identity.IsAuthenticated && httpContext.User.Identity.AuthenticationType.Equals(WIF.AuthenticationTypes.Federation, StringComparison.OrdinalIgnoreCase);
        }


        public override void OnAuthorization(AuthorizationContext filterContext)
        {
             var requiredPermission = String.Format("{0}-{1}", filterContext.ActionDescriptor.ControllerDescriptor.ControllerName, filterContext.ActionDescriptor.ActionName);
             var message = FederatedAuthentication.WSFederationAuthenticationModule.CreateSignInRequest("passive", filterContext.HttpContext.Request.RawUrl, false);
             filterContext.Result = new RedirectResult(message.RequestUrl);

            if (filterContext.RequestContext.HttpContext != null)
            {
                var requestingUser = new CDRLUser(filterContext.RequestContext.HttpContext.User.Identity.Name, filterContext.RequestContext.HttpContext.User.Identity.AuthenticationType);

                if (!requestingUser.HasPermission(requiredPermission) & !requestingUser.IsSysAdmin)
                {

                    filterContext.Result = new RedirectToRouteResult(new RouteValueDictionary { { "action", "Index" }, { "controller", "Unauthorised" } });
                }
            }


Ebenezer


Viewing all articles
Browse latest Browse all 8156

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>