0

I'm in the process of writing my first page in c # APS.Net core 3. I need to block access to it for selected user agent bots, eg Mozilla / 5.0 (compatible; AhrefsBot / 7.0; + http: //ahrefs.com/robot/) . It would be better if you could use the / C # page code. Blocking bots from the robots.txt file is off.

  • Welcome to Stack Overflow. Please take the [tour] to learn how Stack Overflow works and read [ask] on how to improve the quality of your question. Then check the [help/on-topic] to see which questions are on-topic on this site. Please show your attempts you have tried and the problems/error messages you get from your attempts. – Progman Jan 16 '22 at 10:45
  • I want to read the user agent while the page is loading, I don't know how to do it, nor where / in the file should I look for it. sorry – Matt Mcgork Jan 16 '22 at 11:42
  • You might want to check other questions like https://stackoverflow.com/questions/28664770/how-to-get-user-browser-name-user-agent-in-asp-net-core – Progman Jan 16 '22 at 11:45
  • I added the code and the page "404" to be displayed for useragent "bot" as below. app.Use(async (context, next) =>{ var headerUserAgent = context.Request.Headers["User-Agent"].ToString(); if (headerUserAgent == "bot"){ context.Response.Redirect("/404"); } await next(); }); Now I have another problem, after changing the user agent in my browser I get the error "This page is not working The localhost page caused too many redirects." – Matt Mcgork Jan 17 '22 at 10:00

0 Answers0