<commit_before>[![NuGet version](https://badge.fury.io/nu/Codinlab.AspNetCore.Authentication.AdfsOAuth.svg)](https://badge.fury.io/nu/Codinlab.AspNetCore.Authentication.AdfsOAuth)
# AdfsOAuth
ASP.NET Core middleware that enables an application to support the Microsoft ADFS's OAuth 2.0 authentication workflow.
OAuth2 protocol is supported since ADFS 3.0 (Windows Server 2012 R2).

## Client registration on the server
Create the relying party on "AD FS Management", and configure claims...

Then, you have to add your client app using powershell
```PowerShell
Import-Module ADFS
$relyingPartyName = "WebApp"
$appUri = "https://localhost:44303"
 
$clientId = [guid]::NewGuid()
$redirectUri = "$appUri/signin-adfs"
Add-AdfsClient -Name $relyingPartyName -ClientId $clientId -RedirectUri $redirectUri
Write-Host "Client Id: $clientId"
```

## Usage
In your startup.cs
```C#
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseAdfsOAuthAuthentication(new AdfsOAuthOptions("ad.adtest.local")  // Your ADFS server hostname
    {
        ClientUri = "https://localhost:44303",                  // Registered on your ADFS server as Relying party trust
        ClientId = "21495a95-35ef-48ec-80b4-bce8ac430b9f"       // AdfsClient's ClientId registered on your ADFS server
    });
    ...
}

```

By default, the redirect URI is "[*ClientUri*]/signin-adfs". It can be changed in the options.

## A word of warning
As signing certificates are auto-renewed by default by ADFS server, and there is  - for now - no easy way to retrieve the signing certificate from the server, the token issued by the server is **not verfied**.

## References / alternatives
* [Using ADFS 3.0 with MVC 6 (ASP.NET 5)](http://www.carbon60.com/blog/using-adfs-3-0-with-mvc-6-asp-net-5)
* [Authenticating ASP.NET 5 to AD FS OAuth](https://vcsjones.com/2015/05/04/authenticating-asp-net-5-to-ad-fs-oauth/)<commit_msg>Update sample usage<commit_after>
[![NuGet version](https://badge.fury.io/nu/Codinlab.AspNetCore.Authentication.AdfsOAuth.svg)](https://badge.fury.io/nu/Codinlab.AspNetCore.Authentication.AdfsOAuth)
# AdfsOAuth
ASP.NET Core middleware that enables an application to support the Microsoft ADFS's OAuth 2.0 authentication workflow.
OAuth2 protocol is supported since ADFS 3.0 (Windows Server 2012 R2).

## Client registration on the server
Create the relying party on "AD FS Management", and configure claims...

Then, you have to add your client app using powershell
```PowerShell
Import-Module ADFS
$relyingPartyName = "WebApp"
$appUri = "https://localhost:44303"
 
$clientId = [guid]::NewGuid()
$redirectUri = "$appUri/signin-adfs"
Add-AdfsClient -Name $relyingPartyName -ClientId $clientId -RedirectUri $redirectUri
Write-Host "Client Id: $clientId"
```

## Usage
In your startup.cs
```C#
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
    ...
    app.UseAdfsOAuthAuthentication(new AdfsOAuthOptions("ad.adtest.local")  // Your ADFS server hostname
    {
        AutomaticAuthenticate = true,
        ClientUri = "https://localhost:44303",                  // Registered on your ADFS server as Relying party trust
        ClientId = "21495a95-35ef-48ec-80b4-bce8ac430b9f"       // AdfsClient's ClientId registered on your ADFS server
    });
    ...
}

```

By default, the redirect URI is "[*ClientUri*]/signin-adfs". It can be changed in the options.

## A word of warning
As signing certificates are auto-renewed by default by ADFS server, and there is  - for now - no easy way to retrieve the signing certificate from the server, the token issued by the server is **not verfied**.

## References / alternatives
* [Using ADFS 3.0 with MVC 6 (ASP.NET 5)](http://www.carbon60.com/blog/using-adfs-3-0-with-mvc-6-asp-net-5)
* [Authenticating ASP.NET 5 to AD FS OAuth](https://vcsjones.com/2015/05/04/authenticating-asp-net-5-to-ad-fs-oauth/)
{'fraction_non_alphanumeric': 0.11484948140652669, 'fraction_numerical': 0.029850746268656716, 'mean_word_length': 4.049808429118774, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 10, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6971611', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1244, 'n_words': 378}