{"id":20755210,"url":"https://github.com/formix/megacityone-mvc","last_synced_at":"2026-04-18T23:31:35.703Z","repository":{"id":28962578,"uuid":"32488796","full_name":"formix/MegaCityOne-Mvc","owner":"formix","description":"MegaCityOne MVC Authorization Helper Library","archived":false,"fork":false,"pushed_at":"2015-09-09T12:32:24.000Z","size":918,"stargazers_count":0,"open_issues_count":1,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-18T04:26:42.812Z","etag":null,"topics":[],"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/formix.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-03-18T22:57:35.000Z","updated_at":"2015-05-11T19:14:31.000Z","dependencies_parsed_at":"2022-08-17T19:15:13.440Z","dependency_job_id":null,"html_url":"https://github.com/formix/MegaCityOne-Mvc","commit_stats":null,"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/formix%2FMegaCityOne-Mvc","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/formix%2FMegaCityOne-Mvc/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/formix%2FMegaCityOne-Mvc/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/formix%2FMegaCityOne-Mvc/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/formix","download_url":"https://codeload.github.com/formix/MegaCityOne-Mvc/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243059320,"owners_count":20229549,"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":[],"created_at":"2024-11-17T09:23:50.837Z","updated_at":"2025-12-24T23:06:57.014Z","avatar_url":"https://github.com/formix.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MegaCityOne-Mvc\nMegaCityOne MVC 5 Helper Library\n\n[Google Group](http://bit.ly/19bXnDv)\n\nThis library offers an efficient MVC 5 implementation of \n[MegaCityOne](https://github.com/formix/MegaCityOne) security library.\n\n## Installation\n\nMegaCityOne-MVC is available at \n[Nuget.org](https://www.nuget.org/packages/MegaCityOne-Mvc/) and can be \ninstalled as a package using VisualStudio NuGet package manager or via \nNuGet command line:\n\n\u003e Install-Package MegaCityOne.Mvc\n\n# Documentation\n\n* [Api Reference](https://github.com/formix/MegaCityOne-Mvc/blob/master/MegaCityOne.Mvc/doc/api.md)\n\n# Usage\n\n## Authentication\n\nLike specified for [MegaCityOne](https://github.com/formix/MegaCityOne) \nlibrary, you are responsible of validating the user name and password and then\nretrieve user information from your database. \n\n## Configuration\n\nIn App_Start/MegaCityOneActivator.cs Edit the McoDispatcher_Current_Summon method \nto create your Judge and give it to the eventarg.Respondent:\n\n```c#\nstatic void McoDispatcher_Current_Summon(object source, JudgeSummonEventArgs e)\n{\n    // *** instanciate the Judge you need here. *** \\\\\n    JudgeDredd judge = new JudgeDredd();\n    \n    // *** Configure your judge as needed here *** \\\\\n\n    judge.Laws.Add(\"CanCreateProject\", (principal, arguments) =\u003e\n    {\n        // HttpContext is always the first argument whe in MVC \n        // context. Any other arguments can be found after this one.\n        HttpContext httpContext = (HttpContext)arguments[0];\n\n        bool jusgeAnswer = false;\n        // do some coding here\n\n        return judgeAnswer;\n\n    }\n    \n    e.Respondent = judge;\n}\n\n```\n\nSee the complete example [here](https://github.com/formix/MegaCityOne-Mvc/blob/master/MegaCityOne.Mvc.Example/App_Start/MegaCityOneActivator.cs).\n\n## Securing Your Controllers\n\nAdd the `McoAuthenticateAttribute` to your Controller classes. On action methods \nthat do not require authentication, add the `AllowAnonymousAttribute`. To add\nauthorization to your actions, add the `JudgeAuthorizeAttribute(lawName)` \nwith a law name (as defined in the MegaCityOneActivator.cs file) on desired \nactions.\n\n```c#\n[McoAuthenticate]\npublic class HomeController : Controller\n{\n    [AllowAnonymous]\n    public ActionResult Index()\n    {\n        // some code...\n    }\n\n    [HttpPost]\n    [AllowAnonymous]\n    public ActionResult Index(HomeModel model)\n    {\n        // some code...\n    }\n\n    [HttpPost]\n    [AllowAnonymous]\n    public ActionResult Logoff()\n    {\n        // some code...\n    }\n\n    [JudgeAuthorize(\"CanCreateProject\")]\n    public ActionResult CreateProject()\n    {\n        // some code...\n    }\n\n    [JudgeAuthorize(\"CanManageUsers\")]\n    public ActionResult ManageUsers()\n    {\n        // some code...\n    }\n}\n```\n\n## Starting a Security Session\n\nOnce your user has successfully loged in, you should do the folowing in order \nto register it in MegaCityOne Session:\n\n```c#\nMcoCitien citizen = new McoCitizen(\n    \"jhetfield\", // User name\n    new string[] {\"Musician\", \"Composer\", \"Producer\"}); // User roles\n\n// You can add relevant data to the citizen using the \"Data\" property.\ncitizen.Data[\"Label\"] = \"Elektra Records\";\ncitizen.Data[\"Birth\"] = new DateTime(1963, 8, 3);\n\nMcoSession.Login(HttpContext, citizen);\n```\n\n## Ending a Security Session\n\n```c#\nMcoSession.Logout(HttpContext); // Yeah, just that...\n```\n\n### Some Details On MCO Sessions\n\nMegaCityOne.Mvc offers a session manager to keep track of user's security \nsession. The session id (a GUID) is kept in an HttpOnly cookie named \"mco\".\nThis session id is different from the ASP.NET session id. MegaCityOne \nsecurity session information is not kept in the server's Session object but \nin the System.Runtime.Caching.MemoryCache.Default instance. The session \ncontains the given McoCitizen. This gives more flexibility on session \nmanagement. For more security, when a McoCitizen is stored in the cache, we \nsave the requestor ip address in the McoCitizen.Data[\"__host\"] value. \nIf by any means the requestor IP address changes, the current session is \nterminated. When a session is created, it's set to last 24 hours.\n\n## Razor Usage\n\nTo hide (or display) different part of your view, use McoDispatcher static \nhelper class.\n\n```cshtml\n\u003cdiv class=\"panel panel-default\"\u003e\n  \u003cdiv class=\"panel-heading\"\u003e\n    \u003ch3 class=\"panel-title\"\u003eActions\u003c/h3\u003e\n  \u003c/div\u003e\n  \u003cdiv class=\"panel-body\"\u003e\n    @if (McoDispatcher.Advise(\"CanCreateProject\"))\n    {\n      @Html.ActionLink(\"Create Project\", \"CreateProject\", \"Home\");\u003cbr /\u003e\n      ViewBag.ActionAvailable = true;\n    }\n    @if (McoDispatcher.Advise(\"CanManageUsers\"))\n    {\n      @Html.ActionLink(\"Manage Users\", \"ManageUsers\", \"Home\");\u003cbr /\u003e\n      ViewBag.ActionAvailable = true;\n    }\n    @if (!ViewBag.ActionAvailable)\n    {\n      \u003cdiv\u003eThere is no action available for you.\u003c/div\u003e\n    }\n  \u003c/div\u003e\n\u003c/div\u003e\n```\n## Note on Thread Safety\n\nAlways use `McoDispatcher` static class to Advise or Enforce within a Web \napplication. McoDispatcher static methods insure that thread safe calls are \nmade to the Respondent Judge. Judge implementations aren't thread safe.\n\n## Detailed Example\n\nTo get the detailed Mvc example, clone the github project.\n\n\u003e https://github.com/formix/MegaCityOne-Mvc.git\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fformix%2Fmegacityone-mvc","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fformix%2Fmegacityone-mvc","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fformix%2Fmegacityone-mvc/lists"}