Created
September 7, 2019 09:13
-
-
Save alistairjevans/338a06734fd8cfa8b7cb58d617998245 to your computer and use it in GitHub Desktop.
Adding user info middleware
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void Configure(IApplicationBuilder app) | |
{ | |
app.UseStaticFiles(); | |
app.UseCookiePolicy(); | |
app.UseAuthentication(); | |
app.Use(async (ctxt, next) => | |
{ | |
if(ctxt.User == null) | |
{ | |
// Not logged in, so nothing to do. | |
await next(); | |
} | |
else | |
{ | |
// Set a scoped value in the NLog context, then call the next | |
// middleware. | |
var userName = ctxt.User.Identity.Name; | |
using (MappedDiagnosticsLogicalContext.SetScoped("userName", userName)) | |
{ | |
await next(); | |
} | |
} | |
}); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment