{"id":26932858,"url":"https://github.com/robertoborges/democustomauth","last_synced_at":"2025-04-02T08:28:49.124Z","repository":{"id":208305911,"uuid":"721193565","full_name":"RobertoBorges/DemoCustomAuth","owner":"RobertoBorges","description":"Simple demo app that uses Individuals Authentication AspNet Core with a custom Attributes","archived":false,"fork":false,"pushed_at":"2023-12-16T00:47:23.000Z","size":1187,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-04-28T02:06:33.488Z","etag":null,"topics":["asp-net-core","attributes","authentication","authorization","decorators","identity"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RobertoBorges.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2023-11-20T14:47:19.000Z","updated_at":"2024-03-23T12:01:42.000Z","dependencies_parsed_at":"2023-12-16T01:46:48.553Z","dependency_job_id":"46ecca40-3049-4553-9a66-80b4279f53ce","html_url":"https://github.com/RobertoBorges/DemoCustomAuth","commit_stats":null,"previous_names":["robertoborges/democustomauth"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertoBorges%2FDemoCustomAuth","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertoBorges%2FDemoCustomAuth/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertoBorges%2FDemoCustomAuth/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RobertoBorges%2FDemoCustomAuth/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RobertoBorges","download_url":"https://codeload.github.com/RobertoBorges/DemoCustomAuth/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246781062,"owners_count":20832733,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["asp-net-core","attributes","authentication","authorization","decorators","identity"],"created_at":"2025-04-02T08:28:48.617Z","updated_at":"2025-04-02T08:28:49.118Z","avatar_url":"https://github.com/RobertoBorges.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# Individual Authentication demo With Custom Attributes manager\n\n[![.NET](https://github.com/RobertoBorges/DemoCustomAuth/actions/workflows/dotnet.yml/badge.svg)](https://github.com/RobertoBorges/DemoCustomAuth/actions/workflows/dotnet.yml)\n\nThis is a demo of how to use the Individual Authentication on AspNet Core with Razor Pages and Identity.\n\n## How to use\n\nWith Visual Studio 2022, open the solution\n\nApply pending migrations from a command prompt at your project directory under MyIdentityServer project:\n\n\u003e PM\u003e dotnet ef database update\n\nand run the project.\n\nIt should create a local database \"aspnet-DemoCustomAuth\" and open a browser with the demo page.\n\nFor a good test create two users and add some roles on the database.\n\nFor tests use the following users:\n\n| User| Password | Roles |\n| ---- |--- | ----- |\nbob | Pass123$ | Admin\nalice | Pass123$ | User\n\n## How it works\n\nThe demo is based on the default Individual Authentication template with Razor Pages and Identity.\n\nThe main part of this code is about the CustomAttribute class:\n\n```csharp\n\n    public class MyAuthAttribute : Attribute, IAuthorizationFilter\n    {\n\n        public string? Role { get; set; }\n\n        public void OnAuthorization(AuthorizationFilterContext context)\n        {\n            //check access \n            if (CheckPermissions(context))\n            {\n                //all good, add optional code if you want. Or don't\n            }\n            else\n            {\n                //DENIED!\n                //return access denied on the razor page\n                context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { area = \"Identity\", page = \"/Account/AccessDenied\" }));\n            }\n        }\n\n        private bool CheckPermissions(AuthorizationFilterContext context)\n        {\n            if (context.HttpContext.User?.Identity?.IsAuthenticated == true)\n            {\n                //check if user is in role\n                if (!string.IsNullOrEmpty(Role) \u0026\u0026 context.HttpContext.User.IsInRole(Role))\n                {\n                    return true;\n                }\n                //if the user is not in the role, we check if the role is empty\n                //if the role is empty, we allow access\n                else if (!string.IsNullOrEmpty(Role) \u0026\u0026 !context.HttpContext.User.IsInRole(Role))\n                {\n                    return false;\n                }\n                return true;\n            }\n            return false;\n        }\n    }\n```\n\nThis class receives a request when the user hits a Class with the custom Attribute e.g:\n\n```csharp\n[MyAuth(Role = \"Admin\")]\npublic class AdminPageModel : PageModel\n{\n    public void OnGet()\n    {\n\n    }\n}\n```\n\nFrom here you should be able to implement your own logic to check if the user is allowed to access the page or not.\n\nFeel free to use this code as you wish.\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE.txt](LICENSE.txt) file for details\n\n## Acknowledgments\n\nThanks to [StackOverflow](https://stackoverflow.com/) for all the help and to [Microsoft](https://www.microsoft.com/) for the great work on .Net Core and Visual Studio 2022.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertoborges%2Fdemocustomauth","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frobertoborges%2Fdemocustomauth","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frobertoborges%2Fdemocustomauth/lists"}