Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class AuthenticationMiddleware
- {
- private readonly RequestDelegate _next;
- public AuthenticationMiddleware(RequestDelegate next)
- {
- _next = next;
- }
- public async Task Invoke(HttpContext context)
- {
- string authHeader = context.Request.Headers["Authorization"];
- if (authHeader != null && authHeader.StartsWith("Basic"))
- {
- //Extract credentials
- string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
- Encoding encoding = Encoding.GetEncoding("iso-8859-1");
- string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));
- int seperatorIndex = usernamePassword.IndexOf(':');
- var username = usernamePassword.Substring(0, seperatorIndex);
- var password = usernamePassword.Substring(seperatorIndex + 1);
- if (username == "user" && password == "hellofreshgoapi")
- {
- await _next.Invoke(context);
- }
- else
- {
- context.Response.StatusCode = 401; //Unauthorized
- return;
- }
- }
- else
- {
- // no authorization header
- context.Response.StatusCode = 401; //Unauthorized
- return;
- }
- }
- }
Add Comment
Please, Sign In to add comment