|
1 | 1 | # KK.AspNetCore.EasyAuthAuthentication |
| 2 | + |
2 | 3 | This helps getting azure appservice authentication working with asp.net core |
| 4 | + |
| 5 | +> **Caution**: This project is not finished jet! |
| 6 | +
|
| 7 | +## Nuget |
| 8 | + |
| 9 | +The EasyAuth handler is provided as a nuget package and can be found on nuget.org. |
| 10 | + |
| 11 | +| Name | Status | |
| 12 | +| ------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 13 | +| KK.AspNetCore.EasyAuthAuthentication | [](https://www.nuget.org/packages/KK.AspNetCore.EasyAuthAuthentication/) | |
| 14 | + |
| 15 | +You can add the package for example with the following `dotnet` command: |
| 16 | + |
| 17 | +``` |
| 18 | +dotnet add package KK.AspNetCore.EasyAuthAuthentication |
| 19 | +``` |
| 20 | + |
| 21 | +Pre-releases of this Package are pushed to an internal feed an Azure DevOps. There is no public access to this feeds at the moment. |
| 22 | + |
| 23 | +## Build |
| 24 | + |
| 25 | +The build environment for this project is on Azure DevOps and can be found here [dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication](https://dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/_build) |
| 26 | + |
| 27 | +### Nuget package build |
| 28 | + |
| 29 | +| Name | Status | |
| 30 | +| --------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | |
| 31 | +| KK.AspNetCore.EasyAuthAuthentication-CI | [](https://dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/_build/latest?definitionId=24) | |
| 32 | +| Alpha | [](https://dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/_releases2?definitionId=1&_a=releases) | |
| 33 | +| Beta | [](https://dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/_releases2?definitionId=1&_a=releases) | |
| 34 | +| Release | [](https://dev.azure.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/_releases2?definitionId=1&_a=releases) | |
| 35 | + |
| 36 | +### Sample Web build |
| 37 | + |
| 38 | +No build so far. |
| 39 | + |
| 40 | +## Usage |
| 41 | + |
| 42 | +> **INFO**: For detailed usage information please have a look in the `KK.AspNetCore.EasyAuthAuthentication.Sample` project. |
| 43 | +
|
| 44 | +### Startup.cs |
| 45 | + |
| 46 | +Add something like this in the `public void ConfigureServices` method: |
| 47 | + |
| 48 | +``` |
| 49 | +services.AddAuthentication( |
| 50 | + options => |
| 51 | + { |
| 52 | + options.DefaultAuthenticateScheme = EasyAuthAuthenticationDefaults.AuthenticationScheme; |
| 53 | + options.DefaultChallengeScheme = EasyAuthAuthenticationDefaults.AuthenticationScheme; |
| 54 | + } |
| 55 | +).AddEasyAuth(); |
| 56 | +``` |
| 57 | + |
| 58 | +and this to the `public void Configure` method before `app.UseMvc...`: |
| 59 | + |
| 60 | +``` |
| 61 | +app.UseAuthentication(); |
| 62 | +``` |
| 63 | + |
| 64 | +This will enable the `EasyAuthAuthenticationHandler` in your app. |
| 65 | + |
| 66 | +### ...Controller.cs |
| 67 | + |
| 68 | +In your controllers you can access the `User` property as usual: |
| 69 | + |
| 70 | +``` |
| 71 | +[Authorize] |
| 72 | +public string UserName() |
| 73 | +{ |
| 74 | + var mulps = User.HasClaim(ClaimTypes.Name, "user@somecloud.onmicrosoft.com"); |
| 75 | + var peng = User.HasClaim(ClaimTypes.Role, "SystemAdmin"); |
| 76 | + var blubb = HttpContext.User.IsInRole("SystemAdmin"); |
| 77 | + var pop = User.IsInRole("SystemAdmin"); |
| 78 | + return HttpContext.User.Identity.Name; |
| 79 | +} |
| 80 | +``` |
| 81 | + |
| 82 | +### Adding custom roles |
| 83 | + |
| 84 | +If you want to add roles to the `User` property you can have a look in `Transformers/ClaimsTransformer.cs` in the Sample project. There you can see an example how to get started with this. |
| 85 | + |
| 86 | +### Local Debugging |
| 87 | + |
| 88 | +For debugging your application you can place a `me.json` in the `wwwroot/auth` folder of your web app and add some configuration to the `AddEasyAuth` call. |
| 89 | +For example: |
| 90 | + |
| 91 | +``` |
| 92 | +).AddEasyAuth( |
| 93 | + options => |
| 94 | + { |
| 95 | + if (this.Environment.IsDevelopment()) |
| 96 | + { |
| 97 | + options.AuthEndpoint = "auth/me.json"; |
| 98 | + } |
| 99 | + } |
| 100 | +); |
| 101 | +``` |
| 102 | + |
| 103 | +> **Info**: You can obtain the content for this file from an Azure Web App with EasyAuth configured by requesting the `/.auth/me` endpoint. |
| 104 | +
|
| 105 | +## Authors |
| 106 | + |
| 107 | +* **Kirsten Kluge** - *Initial work* - [kirkone](https://github.com/kirkone) |
| 108 | +* **paule96** - *Refactoring* - [paule96](https://github.com/paule96) |
| 109 | +* **Christoph Sonntag** - *Made things even more uber* - [Compufreak345](https://github.com/Compufreak345) |
| 110 | + |
| 111 | +See also the list of [contributors](https://github.com/kirkone/KK.AspNetCore.EasyAuthAuthentication/graphs/contributors) who participated in this project. |
| 112 | + |
| 113 | +## License |
| 114 | + |
| 115 | +This project is licensed under the MIT License - see the [LICENSE.md](LICENSE.md) file for details |
| 116 | + |
| 117 | +## Acknowledgments |
| 118 | + |
| 119 | +* Inspired by this [StackOverflow post](https://stackoverflow.com/a/42402163/6526640) and this [GitHub](https://github.com/lpunderscore/azureappservice-authentication-middleware) repo |
0 commit comments