Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sravimohan/blazor-webassembly-cognito-hosted-ui-sample

Sample Blazor Web assembly project Using Cognito Hosted UI Authentication
https://github.com/sravimohan/blazor-webassembly-cognito-hosted-ui-sample

asp-net asp-net-core aws aws-cognito blazor-webassembly dotnet

Last synced: 26 days ago
JSON representation

Sample Blazor Web assembly project Using Cognito Hosted UI Authentication

Awesome Lists containing this project

README

        

# Sample Blazor Webassembly project Using Cognito Hosted UI Authentication

- Create dotnet WASM project

```dotnet new blazorwasm -au Individual -o BlazorWasmCognitoSample```

This should create a Blazor WASM project with Indivdual type authentication

- [Optional] Create AWS Cognito User Pool.

[Sample Cloudformation - cognito-stack.yml](./.cloudformation/cognito-stack.yml)

[Deploy Script - deploy-cognito-stack.sh](./.cloudformation/deploy-cognito-stack.sh)

If you are using an existing user pool, make sure the CallbackURLs and LogoutURLs match exactly. A common cause of grief is the missing or additional trailing slashes.

Also you will need to configure the Hosted UI.

- Update appsettings.json or appsettings.development.json under wwwroot. Make sure to replace , , with your values.
```
{
"Cognito": {
"Authority": "https://cognito-idp..amazonaws.com/",
"ClientId": "",
"RedirectUri": "https://localhost:5001/authentication/login-callback",
"PostLogoutRedirectUri": "https://localhost:5001/authentication/logout-callback",
"ResponseType": "code"
}
}
```
- Update Program.cs, to refer to the correct config
```
builder.Services.AddOidcAuthentication(options =>
{
builder.Configuration.Bind("Cognito", options.ProviderOptions);
});
```

- Add Authorize Attribute to pages which needs to be protected
```
@using Microsoft.AspNetCore.Authorization

@page "/fetchdata"
@attribute [Authorize]
@inject HttpClient Http

Weather forecast

...
```

- Run
```dotnet run```

Please refer to https://docs.microsoft.com/en-au/aspnet/core/blazor/security/webassembly/?view=aspnetcore-6.0 for official documentation.