Created
December 30, 2016 12:54
-
-
Save alexander-williamson/f26529ee89ce99af9f7e4e8e2ec1cbd7 to your computer and use it in GitHub Desktop.
Examples for simple owin authentication
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
# 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