2

I'm trying to use cookie based authentication in MVC 2.0 with localized URL. In "Configuration" function I'm using:

services.AddAuthentication("MyScheme")
            .AddCookie("MyScheme", options =>
                                   {
                                       options.AccessDeniedPath = "/Auth/Forbidden/";
                                       options.LoginPath = "/Auth/SignIn/";
                                   });

Everything would be fine, but I'm using localized URL like:

template: "{culture}/{controller=Home}/{action=Index}/{id?}"

Is it possible somehow to specify sime pattern in options above to suit the localization URL (like /en/Auth/SignIn/ or other languages)? Right now it is strongy set and this causes problems.

Thank you.

1
  • can you expand on the problems that are caused by the non-localized URL paths? Also, have you tried using route tokens in the AccessDeniedPath/LoginPath fields - e.g., {culture}/Auth/Forbidden? Commented Aug 25, 2017 at 16:17

1 Answer 1

2

The Action<CookieAuthenticationOptions> has the [PathString] LoginPath and [PathString] AccessDeniedPath properties available to it, including the Cookie that is in the process of being constructed (CookieBuilder) and the CookieManager.

I'm not sure if it's absolutely necessary, but I feel fairly confident that you could attach a handler to one or more CookieAuthenticationOptions.Events that re-writes the cookie's path properties accordingly using the provided RedirectContext passed to the handler. See ref - CookieAuthenticationHandler to examine the details of how the path strings are used. In particular, you might look at the CookieAuthenticationEvents:RedirectToAccessDenied and CookieAuthenticationEvents:RedirectToLogin.

That said, I would first want to verify that you're not missing out on a way to get what you want via what's already built-in. Assuming your setup is something like

services.AddMvc(options => { options.Filters.Add(new MiddlewareFilterAttribute(typeof(LocalizationPipeline))); });

I would think that the RouteDataRequestCultureProviderwould handle appending the culture to the /Auth/Forbidden URL that ends up being issued, hinting that there might be a more optimal solution somewhere in how the components are being configured rather than in custom rewriting of paths.

Some additional references:

https://andrewlock.net/url-culture-provider-using-middleware-as-mvc-filter-in-asp-net-core-1-1-0/

https://github.com/aspnet/Security/blob/488eb44467eb677eab62bdc49aa6255cc1be3119/src/Microsoft.AspNetCore.Authentication.Cookies/CookieAuthenticationOptions.cs

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.