0

I've prepared the authentication like this in my Start.cs.

...
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
  .AddJwtBearer(a => a.TokenValidationParameters = new TokenValidationParameters
  {
    ValidateIssuer = true,
    ValidateAudience = true,
    ValidateLifetime = true,
    ValidateIssuerSigningKey = true,
    ValidIssuer = "beep",
    ValidAudience = "bopp",
    IssuerSigningKey = new SymmetricSecurityKey(
    Encoding.UTF8.GetBytes("SecurityKeyOfProperLength"))
  });
...

When I decorate one of the methods in my controller using [Authorize] attribute, I no longer get the view it points to rendered. Instead, just as expected, I get 401 Unauthorized.

The next step I want to do is to be redirected to my login page, which is located in the controller Security and method Login. I don't know how to achieve it properly with no Q&D.

What I've tried to do is to google, of course, and I've found out a lot of examples on how to handle that from SPA. In my case it's good, old, plain MVC rendered on the backend so that's not applicable, as far I understand.

I've also seen that I can override OnChallenge but it wasn't recommended.

A bunch of links referred to 3rd parties like IDS and such. In my case, I want to learn the process and control it by micromanagement, at least for now. Mostly, because I want to learn that in depth.

How do I configure my AddAuthentication so that it bounces the user to the right controller and method (still using best practices)? A few hints on terms to look for would by great - I'm not lazy, just careful and uncertain of what's good stuff and what's other confused bloggers' production.

DonkeyBanana
  • 2,304
  • 18
  • 48
  • you can override HandleUnauthorizedRequestand redirect then you have to mark the controller actions to use – Arunprasanth K V Jan 28 '19 at 10:23
  • @ArunprasanthKV OK, so I've been googling it some more, based on your [suggestion about *HandleUnauthorizedRequestand*](https://docs.microsoft.com/en-us/dotnet/api/system.web.mvc.authorizeattribute.handleunauthorizedrequest?view=aspnet-mvc-5.2) but I can't seem to override that method, it doesn't seem to exist. I googled [how to override it](https://stackoverflow.com/questions/40446028/how-to-override-handleunauthorizedrequest-in-asp-net-core) but the answer to that didn't even use said method. I'm back at status *immensely uncertain*. Please advise... – DonkeyBanana Jan 29 '19 at 23:03
  • just have a look on this https://stackoverflow.com/questions/10928277/redirect-to-another-page-when-user-is-not-authorized-in-asp-net-mvc3 – Arunprasanth K V Jan 30 '19 at 05:59

0 Answers0