MVC 5 How to define Owin LoginPath with localized routes

前端 未结 8 660
有刺的猬
有刺的猬 2020-12-12 19:18

I have a MVC 5 website with localized routes defined as

routes.MapRoute(
            name: \"Default\",
            url: \"{culture}/{controller}/{action}/{         


        
相关标签:
8条回答
  • 2020-12-12 19:49

    Without taking too much of responsibility on the url format etc, you can do something like the following

    public static void Configuration(IAppBuilder app)
    {
        UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString(url.Action("LogOn", "Account", new { area = "Account" })),
            Provider = new CookieAuthenticationProvider
            {
                OnApplyRedirect = context => context.Response.Redirect(context.RedirectUri.Replace(CultureHelper.GetDefaultCulture(), Thread.CurrentUiCulture.Name))
            }
        });
    }
    
    0 讨论(0)
  • 2020-12-12 20:00

    The solution can be even simpler if you put the culture on the loginpath :

    app.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        //Your other properties here
        LoginPath = new PathString("/{culture}/User/Login")
    });
    
    0 讨论(0)
提交回复
热议问题