• .net Core 简单中间件使用


    在项目文件夹中定义一个 Middleware 文件夹,建一个中间件类

    using Microsoft.AspNetCore.Http;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    
    namespace WebOclot
    {
        // 定义一个中间件类型
        public class UserValidate
        {
            private RequestDelegate _next;
            public UserValidate(RequestDelegate next)
            {
                _next = next;  // 下一个中间件
            }
    
            public async Task InvokeAsync(HttpContext context)
            {
                string method = context.Request.Method;
                string userName = "";
                string Pwd = "";
                if (method == "GET")
                {
                    userName = context.Request.Query["UserName"];
                    Pwd = context.Request.Query["Pwd"];
                }
                else if (method == "POST")
                {
                    userName = context.Request.Form["UserName"];
                    Pwd = context.Request.Form["Pwd"];
                }
                if (userName == "zs" && Pwd == "123")  // 验证是否通过
                {
                    await _next(context);   // 往下执行
                }
                else
                {
                    await context.Response.WriteAsync("用户名或密码不正确");
                }
            }
        }
    
    
    }

    注册中间件

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Microsoft.Extensions.Options;
    using Ocelot.DependencyInjection;
    using Ocelot.Middleware;
    using Ocelot.Provider.Consul;
    using Ocelot.Provider.Polly;
    
    namespace WebOclot
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddOcelot().AddConsul().AddPolly();
                // 删除掉此处所有默认的配置
    
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                app.UseMiddleware<UserValidate>();
                app.UseOcelot();
                // 删除掉此处所有默认的配置
            }
        }
    }
  • 相关阅读:
    Devpexpress 打印预览问题
    常用DOS命令
    C# datetimePicker控件格式设置
    DevExpress中GridControl的属性设置
    C++ 学习1
    layui 时间控件选择一闪就消失,打不开问题解决办法
    echarts报错,Uncaught Error: series.type should be specified?
    vue项目中,在mian.js文件中引入scss文件后,报错
    记一次在vue中使用scss报错
    使用hexo搭建个人博客时引入图片失败
  • 原文地址:https://www.cnblogs.com/yingger/p/13547370.html
Copyright © 2020-2023  润新知