{"id":13591741,"url":"https://github.com/damienbod/bff-aspnetcore-vuejs","last_synced_at":"2025-04-19T07:30:01.845Z","repository":{"id":197720760,"uuid":"695754038","full_name":"damienbod/bff-aspnetcore-vuejs","owner":"damienbod","description":"Backend for frontend security using Vue.js Typescript, Vite, ASP.NET Core backend and Microsoft Entra ID","archived":false,"fork":false,"pushed_at":"2024-12-18T19:11:49.000Z","size":638,"stargazers_count":37,"open_issues_count":1,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-16T03:55:44.135Z","etag":null,"topics":["aspnetcore","bff","dotnet","entra","entraid","microsoft-identity","msal","oidc","openid-connect","typescript","vite","vue"],"latest_commit_sha":null,"homepage":"https://damienbod.com/2023/10/02/implement-a-secure-web-application-using-vue-js-and-an-asp-net-core-server/","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/damienbod.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2023-09-24T05:41:10.000Z","updated_at":"2025-03-06T01:58:13.000Z","dependencies_parsed_at":null,"dependency_job_id":"fba8ae7e-bbb8-4004-b209-47d47424e166","html_url":"https://github.com/damienbod/bff-aspnetcore-vuejs","commit_stats":null,"previous_names":["damienbod/bff-aspnetcore-vuejs"],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damienbod%2Fbff-aspnetcore-vuejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damienbod%2Fbff-aspnetcore-vuejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damienbod%2Fbff-aspnetcore-vuejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/damienbod%2Fbff-aspnetcore-vuejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/damienbod","download_url":"https://codeload.github.com/damienbod/bff-aspnetcore-vuejs/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249638245,"owners_count":21304304,"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":["aspnetcore","bff","dotnet","entra","entraid","microsoft-identity","msal","oidc","openid-connect","typescript","vite","vue"],"created_at":"2024-08-01T16:01:01.467Z","updated_at":"2025-04-19T07:30:01.819Z","avatar_url":"https://github.com/damienbod.png","language":"C#","funding_links":[],"categories":["C#","C\\#"],"sub_categories":[],"readme":"# bff-aspnetcore-vuejs\n\n[![.NET and npm build](https://github.com/damienbod/bff-aspnetcore-vuejs/actions/workflows/dotnet.yml/badge.svg)](https://github.com/damienbod/bff-aspnetcore-vuejs/actions/workflows/dotnet.yml) [![Build and deploy to Azure Web App](https://github.com/damienbod/bff-aspnetcore-vuejs/actions/workflows/azure-webapps-dotnet-core.yml/badge.svg)](https://github.com/damienbod/bff-aspnetcore-vuejs/actions/workflows/azure-webapps-dotnet-core.yml) [![License](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg)](https://github.com/damienbod/bff-aspnetcore-vuejs/blob/main/LICENSE)\n\n[Implement a secure web application using Vue.js and an ASP.NET Core server](https://damienbod.com/2023/10/02/implement-a-secure-web-application-using-vue-js-and-an-asp-net-core-server/)\n\n## Setup Server \n\nThe ASP.NET Core project is setup to run in development and production. In production, it uses the Vue.js production build deployed to the wwwroot. In development, it uses MS YARP reverse proxy to forward requests.\n\n\u003e [!IMPORTANT]  \n\u003e In production, the Vue.js project is built into the **wwwroot** of the .NET project.\n\n![BFF production](https://github.com/damienbod/bff-aspnetcore-vuejs/blob/main/images/vue-aspnetcore-bff_01.png)\n\nConfigure the YARP reverse proxy to match the Vue.js URL. This is only required in development. I always use HTTPS in development and the port needs to match the Vue.js developement env (vite.config.js).\n\n```json\n \"UiDevServerUrl\": \"https://localhost:4201\",\n  \"ReverseProxy\": {\n    \"Routes\": {\n      \"route1\": {\n        \"ClusterId\": \"cluster1\",\n        \"Match\": {\n          \"Path\": \"{**catch-all}\"\n        }\n      }\n    },\n    \"Clusters\": {\n      \"cluster1\": {\n        \"HttpClient\": {\n          \"SslProtocols\": [\n            \"Tls12\"\n          ]\n        },\n        \"Destinations\": {\n          \"cluster1/destination1\": {\n            \"Address\": \"https://localhost:4201/\"\n          }\n        }\n      }\n    }\n  }\n```\n\n\n## Setup Vue.js Vite project\n\nAdd the certificates to the nx project for example in the **/certs** folder\n\nUpdate the vite.config.ts file:\n\n```typescipt\nimport { defineConfig } from 'vite'\nimport vue from '@vitejs/plugin-vue'\nimport fs from 'fs';\n\n// https://vitejs.dev/config/\nexport default defineConfig({\n  plugins: [vue()],\n  server: {\n    https: {\n      key: fs.readFileSync('./certs/dev_localhost.key'),\n      cert: fs.readFileSync('./certs/dev_localhost.pem'),\n\t},\n    port: 4202,\n    strictPort: true, // exit if port is in use\n    hmr: {\n      clientPort: 4202, // point vite websocket connection to vite directly, circumventing .net proxy\n    },\n  },\n  optimizeDeps: {\n    force: true,\n  },\n  build: {\n    outDir: \"../server/wwwroot\",\n    emptyOutDir: true\n  },\n})\n```\n\n\n\u003e [!NOTE]  \n\u003e The ASP.NET Core project setup uses port 4202, this needs to match the YARP reverse proxy settings for development.\n\n\n## Setup development\n\nThe development environment is setup to use the defualt tools for each of the tech stacks. Vue.js is used like recommended. I use Visual Studio code. A YARP reverse proxy is used to integrate the Vue.js development into the backend application.\n\n![BFF development](https://github.com/damienbod/bff-aspnetcore-vuejs/blob/main/images/vue-aspnetcore-bff-yarp-dev_01.png)\n\n\u003e [!NOTE]  \n\u003e Always run in HTTPS, both in development and production\n\n```\nnpm start\n```\n\n## Azure App Registration setup\n\nThe application(s) are deployed as one. This is an OpenID Connect confidential client with a user secret or a certification for client assertion.\n\nUse the Web client type on setup.\n\n![BFF Azure registration](https://github.com/damienbod/bff-aspnetcore-angular/blob/main/images/azure-app-registration_01.png)\n\nThe OpenID Connect client is setup using **Microsoft.Identity.Web**. This implements the Microsoft Entra ID client. I have created downstream APIs using the OBO flow and a Microsoft Graph client. This could be replaced with any OpenID Connect client and requires no changes in the frontend part of the solution.\n\n```csharp\nvar scopes = configuration.GetValue\u003cstring\u003e(\"DownstreamApi:Scopes\");\nstring[] initialScopes = scopes!.Split(' ');\n\nservices.AddMicrosoftIdentityWebAppAuthentication(configuration, \"MicrosoftEntraID\")\n    .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)\n    .AddMicrosoftGraph(\"https://graph.microsoft.com/v1.0\", initialScopes)\n    .AddInMemoryTokenCaches();\n\nservices.AddControllersWithViews(options =\u003e\n    options.Filters.Add(new AutoValidateAntiforgeryTokenAttribute()));\n\nservices.AddRazorPages().AddMvcOptions(options =\u003e\n{\n    //var policy = new AuthorizationPolicyBuilder()\n    //    .RequireAuthenticatedUser()\n    //    .Build();\n    //options.Filters.Add(new AuthorizeFilter(policy));\n}).AddMicrosoftIdentityUI();\n```\n\nAdd the Azure App registration settings to the **appsettings.Development.json** and the **ClientSecret** to the user secrets.\n\n```json\n\"MicrosoftEntraID\": {\n    \"Instance\": \"https://login.microsoftonline.com/\",\n    \"Domain\": \"[Enter the domain of your tenant, e.g. contoso.onmicrosoft.com]\",\n    \"TenantId\": \"[Enter 'common', or 'organizations' or the Tenant Id (Obtained from the Azure portal. Select 'Endpoints' from the 'App registrations' blade and use the GUID in any of the URLs), e.g. da41245a5-11b3-996c-00a8-4d99re19f292]\",\n    \"ClientId\": \"[Enter the Client Id (Application ID obtained from the Azure portal), e.g. ba74781c2-53c2-442a-97c2-3d60re42f403]\",\n    \"ClientSecret\": \"[Copy the client secret added to the app from the Azure portal]\",\n    \"ClientCertificates\": [\n    ],\n    // the following is required to handle Continuous Access Evaluation challenges\n    \"ClientCapabilities\": [ \"cp1\" ],\n    \"CallbackPath\": \"/signin-oidc\"\n},\n```\n\nApp Service (linux plan) configuration \n\n```\nMicrosoftEntraID__Instance               --your-value--\nMicrosoftEntraID__Domain                 --your-value--\nMicrosoftEntraID__TenantId               --your-value--\nMicrosoftEntraID__ClientId               --your-value--\nMicrosoftEntraID__CallbackPath           /signin-oidc\nMicrosoftEntraID__SignedOutCallbackPath  /signout-callback-oidc\n```\n\nThe client secret or client certificate needs to be setup, see Microsoft Entra ID documentation.\n\n## Debugging\n\nStart the Vue.js project from the **ui** folder\n\n```\nnpm start\n```\n\nStart the ASP.NET Core project from the **server** folder\n\n```\ndotnet run\n```\n\nOr just open Visual Studio and run the solution.\n\n## github actions build\n\nGithub actions is used for the DevOps. The build pipeline builds both the .NET project and the Vue.js project using npm. The two projects are built in the same step because the UI project is built into the wwwroot of the server project.\n\n```yaml\n\nname: .NET and npm build\n\non:\n  push:\n    branches: [ \"main\" ]\n  pull_request:\n    branches: [ \"main\" ]\n\njobs:\n  build:\n    runs-on: ubuntu-latest\n\n    steps:\n\n      - uses: actions/checkout@v4\n      - name: Setup .NET\n        uses: actions/setup-dotnet@v4\n        with:\n          dotnet-version: 8.0.x\n\n      - name: Restore dependencies\n        run: dotnet restore\n\n      - name: npm setup\n        working-directory: ui\n        run: npm install\n\n      - name: ui-build\n        working-directory: ui\n        run: npm run build\n\n      - name: Build\n        run: dotnet build --no-restore\n      - name: Test\n        run: dotnet test --no-build --verbosity normal\n```\n\n## github actions Azure deployment\n\nThe deployment pipeline builds both projects and deployes this to Azure using an Azure App Service. See **azure-webapps-dotnet-core.yml**\n\ndeployment test server: https://bff-vue-aspnetcore.azurewebsites.net/\n\n## Credits and used libraries\n\n- NetEscapades.AspNetCore.SecurityHeaders\n- Yarp.ReverseProxy\n- Microsoft.Identity.Web\n- ASP.NET Core\n- Vue.js\n- Vite\n\n## Links\n\nhttps://vuejs.org/\n\nhttps://vitejs.dev/\n\nhttps://github.com/vuejs/create-vue\n\nhttps://learn.microsoft.com/en-us/aspnet/core/introduction-to-aspnet-core\n\nhttps://github.com/AzureAD/microsoft-identity-web\n\nhttps://www.koderhq.com/tutorial/vue/vite/\n\nhttps://github.com/isolutionsag/aspnet-react-bff-proxy-example\n\nhttps://github.com/damienbod/bff-aspnetcore-angular\n\nhttps://github.com/damienbod/bff-auth0-aspnetcore-angular\n\nhttps://github.com/damienbod/bff-openiddict-aspnetcore-angular\n\nhttps://github.com/damienbod/bff-azureadb2c-aspnetcore-angular\n\nhttps://github.com/damienbod/bff-MicrosoftEntraExternalID-aspnetcore-angular\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamienbod%2Fbff-aspnetcore-vuejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdamienbod%2Fbff-aspnetcore-vuejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdamienbod%2Fbff-aspnetcore-vuejs/lists"}