Middleware使用
#region Middleware使用
{
IApplicationBuilder applicationBuilder
= new IApplicationBuilder();
applicationBuilder
.UseAuthentication();
applicationBuilder
.UseAuthorization();
applicationBuilder
.UseException();
Middleware middleware
= applicationBuilder
.Build();
HttpContext httpContext
= new HttpContext();
httpContext
.httpRequest
= new HttpRequest() { requestUrl
= "index.html" };
middleware
.HandlerRequest(httpContext
);
}
#endregion
Application应用相关类
class IApplicationBuilder
{
private IApplication application
= new IApplication();
public IApplicationBuilder UseException()
{
Middleware middleware
= new ExceptionMiddleware();
application
.middlewares
.Add(middleware
);
return this;
}
public IApplicationBuilder UseAuthentication()
{
Middleware middleware
= new AuthenticationMiddleware();
application
.middlewares
.Add(middleware
);
return this;
}
public IApplicationBuilder UseAuthorization()
{
Middleware middleware
= new AuthorizationMiddleware();
application
.middlewares
.Add(middleware
);
return this;
}
public Middleware Build()
{
Middleware middlewareFrist
= new Default404Middleware();
List
<Middleware
> middlewares
= application
.middlewares
;
middlewares
.Reverse();
foreach (Middleware middleware
in middlewares
)
{
middlewareFrist
.nextMiddleware
= middleware
;
middlewareFrist
= middleware
;
}
return middlewareFrist
;
}
}
class IApplication
{
public List
<Middleware
> middlewares
{ set; get; } = new List<Middleware>();
}
权限验证相关类
class CookieAuthentication : IAuthentication
{
public void Authentica(HttpContext httpContext
)
{
Console
.WriteLine($
"Cookie身份认证");
}
}
interface IAuthentication
{
public void Authentica(HttpContext httpContext
);
}
class JwtIAuthentication : IAuthentication
{
public void Authentica(HttpContext httpContext
)
{
Console
.WriteLine($
"Jwt身份认证");
}
}
class NullAuthentication : IAuthentication
{
public void Authentica(HttpContext httpContext
)
{
Console
.WriteLine($
"身份认证不存在");
}
}
相关中间件
class AuthenticationMiddleware : Middleware
{
private string token
= "authen_token";
public override void HandlerRequest(HttpContext httpContext
)
{
Console
.WriteLine($
"执行身份认证中间件");
if (httpContext
.httpRequest
.requestUrl
.Contains(token
))
{
Console
.WriteLine($
"身份认证中间件终止了");
return;
}
else
{
nextMiddleware
.HandlerRequest(httpContext
);
}
}
}
class AuthorizationMiddleware : Middleware
{
private string token
= "author_token";
public override void HandlerRequest(HttpContext httpContext
)
{
Console
.WriteLine($
"执行授权中间件");
if (httpContext
.httpRequest
.requestUrl
.Contains(token
))
{
Console
.WriteLine($
"授权中间件终止了");
return;
} else
{
nextMiddleware
.HandlerRequest(httpContext
);
}
}
}
class Default404Middleware : Middleware
{
private string token
= "404";
public override void HandlerRequest(HttpContext httpContext
)
{
Console
.WriteLine($
"执行默认404中间件");
if (httpContext
.httpRequest
.requestUrl
.Contains(token
))
{
Console
.WriteLine($
"默认40中间件终止了");
return;
}
else
{
nextMiddleware
.HandlerRequest(httpContext
);
}
}
}
class ExceptionMiddleware : Middleware
{
private string token
= "exception";
public override void HandlerRequest(HttpContext httpContext
)
{
Console
.WriteLine($
"执行异常中间件");
if (httpContext
.httpRequest
.requestUrl
.Contains(token
))
{
Console
.WriteLine($
"异常中间件终止了");
return;
}
else
{
nextMiddleware
.HandlerRequest(httpContext
);
}
}
}
abstract class Middleware
{
public Middleware nextMiddleware
{ set; get; }
public abstract void HandlerRequest(HttpContext httpContext
);
}
HTTP相关类
class HttpContext
{
public HttpRequest httpRequest
{ set; get; }
public HttpResponse httpResponse
{ set; get; }
}
class HttpRequest
{
public string requestUrl
;
}
class HttpResponse
{
}
转载请注明原文地址: https://lol.8miu.com/read-20114.html