Skip to content

Instantly share code, notes, and snippets.

@alexander-williamson
Created December 30, 2016 12:54
Show Gist options
  • Save alexander-williamson/f26529ee89ce99af9f7e4e8e2ec1cbd7 to your computer and use it in GitHub Desktop.
Save alexander-williamson/f26529ee89ce99af9f7e4e8e2ec1cbd7 to your computer and use it in GitHub Desktop.
Examples for simple owin authentication
# Your use statement can do all the work for you
app.Use(async (context, next) =>
{
if (!IsAuthenticated(context))
{
context.Response.StatusCode = 401;
var bytes = System.Text.Encoding.UTF8.GetBytes("You are not authorised!");
context.Response.Body.Write(bytes, 0, bytes.Length);
await Task.FromResult(context);
}
else
{
await next();
}
});
# Or authorization on each get
app.MapWhen(whenContext => {
return IsAuthenticated && whenContext.Request.Method == "GET" && whenContext.Request.Uri.IsMatch("/helloworld")
}, env => {
env.Use(async (context, next) =>
{
var bytes = System.Text.Encoding.UTF8.GetBytes("Hello world! This will return a 200 status code by default.");
context.Response.Body.Write(bytes, 0, bytes.Length);
await next();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment