Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…etcore into main
  • Loading branch information
Daniele Giallonardo committed Aug 31, 2021
2 parents 3853c7b + ac92ba7 commit a5c12f0
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public void ConfigureServices(IServiceCollection services)
})
.AddCie(Configuration, o => {
o.Events.OnTokenCreating = async (s) => await s.HttpContext.RequestServices.GetRequiredService<CustomCieEvents>().TokenCreating(s);
o.Events.OnAuthenticationSuccess = async (s) => await s.HttpContext.RequestServices.GetRequiredService<CustomCieEvents>().AuthenticationSuccess(s);
o.LoadFromConfiguration(Configuration);
})
.AddCookie();
Expand All @@ -144,6 +145,75 @@ public class CustomCieEvents : CieEvents

return base.TokenCreating(context);
}

public override Task AuthenticationSuccess(AuthenticationSuccessContext context)
{
var principal = context.Principal;

// Recupero dati provenienti da Cie da ClaimsPrincipal
var name = principal.FindFirst(CieClaimTypes.Name);
var familyName = principal.FindFirst(CieClaimTypes.FamilyName);
var email = principal.FindFirst(CieClaimTypes.Email);
var dateOfBirth = principal.FindFirst(CieClaimTypes.DateOfBirth);

return base.AuthenticationSuccess(context);
}
}
```

# Error Handling
La libreria può, in qualunque fase (sia in fase di creazione della Request sia in fase di gestione della Response), sollevare eccezioni.
Un tipico scenario è quello in cui vengono ricevuti i codici di errore previsti dal protocollo (n.19, n.20, ecc....), in tal caso la libreria solleva un'eccezione contenente il corrispondente messaggio d'errore localizzato, richiesto dalle specifiche CIE3.0, che è possibile gestire (ad esempio per la visualizzazione) utilizzando il normale flusso previsto per AspNetCore. L'esempio seguente fa uso del middleware di ExceptionHandling di AspNetCore.

```csharp
public void Configure(IApplicationBuilder app, IHostEnvironment env)
{
...
app.UseExceptionHandler("/Home/Error");
...
}

.......

// HomeController
[AllowAnonymous]
public async Task<IActionResult> Error()
{
var exceptionHandlerPathFeature =
HttpContext.Features.Get<IExceptionHandlerPathFeature>();

string errorMessage = string.Empty;

if (exceptionHandlerPathFeature?.Error != null)
{
var messages = FromHierarchy(exceptionHandlerPathFeature?.Error, ex => ex.InnerException)
.Select(ex => ex.Message)
.ToList();
errorMessage = String.Join(" ", messages);
}

return View(new ErrorViewModel
{
RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier,
Message = errorMessage
});
}

private IEnumerable<TSource> FromHierarchy<TSource>(TSource source,
Func<TSource, TSource> nextItem,
Func<TSource, bool> canContinue)
{
for (var current = source; canContinue(current); current = nextItem(current))
{
yield return current;
}
}

private IEnumerable<TSource> FromHierarchy<TSource>(TSource source,
Func<TSource, TSource> nextItem)
where TSource : class
{
return FromHierarchy(source, nextItem, s => s != null);
}
```

Expand Down

0 comments on commit a5c12f0

Please sign in to comment.