简体   繁体   中英

ASP.NET Core Own middleware component

How to get the second argument from HTTP Context? For example

localhost:45423/?login=login1/?password=password1

  • Here is what I coded but it doesn't work. It always says that I'm not logged even when I inputted the correct login and password
public class CheckData
    {
        private readonly RequestDelegate _next;

        public CheckData(RequestDelegate next)
        {
            _next = next;
        }
        public async Task InvokeAsync(HttpContext context)
        {
            var login = context.Request.Query["login"];
            var password = context.Request.Query["password"];
            if(login != "log1" && password != "pass1")
            {
                context.Response.StatusCode = 403;
                await context.Response.WriteAsync("Sorry, you're not logged");
            }
            else
            {
                await _next.Invoke(context);
            }
        }
    }

PS: I've began learning ASP.NET Core today, only login works, but not login with password

You are passing the query parameters wrongly, For multiple query parameter you should pass like localhost:45423?login=login1&password=password1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM