当前位置:网站首页>. Net core uses basic authentication Middleware
. Net core uses basic authentication Middleware
2022-07-16 08:04:00 【Hometown 2130】
1. Establish a working .net core web api project , And can run .

2. increase BasicMiddleware class
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApplication6
{
public class BasicMiddleware
{
private readonly RequestDelegate _next;
public const string AuthorizationHeader = "Authorization";
public const string WWWAuthenticateHeader = "WWW-Authenticate";
private BasicUser _user;
public BasicMiddleware(RequestDelegate next, BasicUser user)
{
_next = next;
_user = user;
}
public Task Invoke(HttpContext httpContext)
{
var Request = httpContext.Request;
string auth = Request.Headers[AuthorizationHeader];
if (auth == null)
{
return BasicResult(httpContext);
}
// obtain Base64 And decode it into a string
string[] authParts = auth.Split(' ');
if (authParts.Length != 2)
return BasicResult(httpContext);
string base64 = authParts[1];
string authValue;
try
{
byte[] bytes = Convert.FromBase64String(base64);
authValue = Encoding.ASCII.GetString(bytes);
}
catch
{
authValue = null;
}
if (string.IsNullOrEmpty(authValue))
return BasicResult(httpContext);
// Resolve user name and password
string userName;
string password;
int sepIndex = authValue.IndexOf(':');
if (sepIndex == -1)
{
userName = authValue;
password = string.Empty;
}
else
{
userName = authValue.Substring(0, sepIndex);
password = authValue.Substring(sepIndex + 1);
}
// Determine the user name and password
if (_user.UserName.Equals(userName) && _user.Password.Equals(password))
return _next(httpContext);
else
return BasicResult(httpContext);
}
/// <summary>
/// Return required Basic Authentication output
/// </summary>
/// <param name="httpContext"></param>
/// <returns></returns>
private static Task BasicResult(HttpContext httpContext)
{
httpContext.Response.StatusCode = 401;
httpContext.Response.Headers.Add(WWWAuthenticateHeader, "Basic realm=\"localhost\"");
return Task.FromResult(httpContext);
}
}
public static class BasicMiddlewareExtensions
{
public static IApplicationBuilder UseBasicMiddleware(this IApplicationBuilder builder, BasicUser user)
{
if (user == null)
throw new ArgumentException(" Need to set up Basic user ");
return builder.UseMiddleware<BasicMiddleware>(user);
}
}
}
3. establish BasicUser class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace WebApplication6
{
public class BasicUser
{
public string UserName { get; set; }
public string Password { get; set; }
}
}
4. stay Startup.cs Class Configure Method using middleware , And set the account and password

app.UseBasicMiddleware(new BasicUser {
UserName="admin", Password="123456"
});5. Run URL , Click on the perform , bounced

6. Enter the account and password

7. effect , Successfully return the value

Generally, this kind of authentication is relatively weak , have access to JWT Instead of .
边栏推荐
猜你喜欢
随机推荐
ELECTRA
Installing redis on Linux
js基础:js清空数组元素极简代码示例
CCF 201909-1 scale detection point query
Day 17 of leetcode
老板招了个有6年经验的测试员,让我见识到了什么是天花板...
neo4j学习资料整理
Nodejs+express set and get cookies, session
OSPF experiment
Research on domestic and foreign speech synthesis companies
4年测试工程师经历,下一步转开发还是继续测试?
64 bit integer division multiplication
V891-Z3735F重做系统,驱动修复汇总
Rust Thrift Demo
About pycharm localization 2020-09-20
I2C协议
HCIA复习
Puppeteer之提高UI层测试可读性
Day 8 of leetcode question brushing
C#笔记-基础知识,问答,WPF








