Coder Social home page Coder Social logo

Comments (18)

Dresel avatar Dresel commented on June 29, 2024

I tried to reproduce with the Sample Project.

I added Microsoft.Owin.Host.SystemWeb and Microsoft.AspNet.Identity.Owin. This is my StartUp Configuration:

public class StartUp
{
    public void Configuration(IAppBuilder appBuilder)
    {
        UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);

        appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString(url.Action(MVC.Home.Index()))
        });
    }
}

I decorated HomeWithRoutePrefixAttributeController with [Authorize]. When accessing HomeWithRouteArea/Welcome I'm redirected to Welcome?ReturnUrl=%2Fen%2FHomeWithRouteArea%2FWelcome. Am I something missing?

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

Are you using culture prefix in the routes? The onde way I was able to make
this work was setting the login path to '/pt/entrar' instead of getting
from the action. But in this case i am not able to localize it.
Em 11/07/2014 07:11, "Dresel" [email protected] escreveu:

I tried to reproduce with the Sample Project.

I added Microsoft.Owin.Host.SystemWeb and
Microsoft.AspNet.Identity.Owin. This is my StartUp Configuration:

public class StartUp
{
public void Configuration(IAppBuilder appBuilder)
{
UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);

    appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString(url.Action(MVC.Home.Index()))
    });
}

}

I decorated HomeWithRoutePrefixAttributeController with [Authorize].
When accessing HomeWithRouteArea/Welcome I'm redirected to
Welcome?ReturnUrl=%2Fen%2FHomeWithRouteArea%2FWelcome. Am I something
missing?

—
Reply to this email directly or view it on GitHub
#16 (comment)
.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

If I understand correctly you want to be redirected to the localized login method?

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

Yes, because the login will bĂŞ made by people that will only understand one
of the languages.
Em 11/07/2014 09:39, "Dresel" [email protected] escreveu:

If I understand correctly you want to be redirected to the localized login
method?

—
Reply to this email directly or view it on GitHub
#16 (comment)
.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

Well if you set LoginPath, this is a fixed path for all requests. You could use OnApplyRedirect to dynamically redirect to a localized route:

appBuilder.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString(url.Action(MVC.User.Login())),
    Provider =
        new CookieAuthenticationProvider()
        {
            OnApplyRedirect = 
                context => context.Response.Redirect(
                    url.Action(MVC.User.Login().AddRouteValues(new { culture = Thread.CurrentThread.CurrentCulture.Name })))
        }
});

Here you would be redirected to the current thread culture which could e.g. be set by CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate.

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

That was something I tried ... I do have the following on Application_Start:

CultureSensitiveHttpModule.GetCultureFromHttpContextDelegate = context => { return context.GetCulture(); };

And to test I used the extension as follows:

public static CultureInfo GetCulture(this HttpContext context) {
  return new CultureInfo("pt");

But when I try to access an action with Authorize I keep getting the 401 error and not being redirected ...

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

What URL is generated - you will get the 401 error if it does not exist.

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

The user signin action is as follows:

[Route("entrar"), HttpGet]
public virtual ActionResult SignIn() { }

I am able to access it using, as expected due to the translation I am using:
/pt/entrar
/en/signin

I typed both in the URL and they work ... And I get the right culture in each one.

The action with Authorize attribute is:

[RouteArea("cms", AreaPrefix = "cms")]
public partial class HomeController : Controller {
  [Route("inicio", Name = "cms.home.index"), HttpGet] { }
}

I am able to access it, after being autenticathed, typing in the url bar:
pt/cms/inicio

This is the only route I have for CMS home page. I am not translation the CMS routes ...

In fact the prefix didn't needed to be added to the CMS routes.

Basically this is a situation where the website is in many languages but the CMS only in one.

But the login page should be in different languages because there are reserved areas which are also in different languages.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

What URL does

url.Action(MVC.User.Login().AddRouteValues(new { culture = Thread.CurrentThread.CurrentCulture.Name }))

in OnApplyRedirect produce?

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

When I try:

var test = url.Action(MVC.User.SignIn().AddRouteValues(new { culture = Thread.CurrentThread.CurrentCulture.Name }));

On owin setup test is null ... Not sure if this is what you were asking.

Are translated routes already available when Owin starts?

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

I don't know - my StartUp is called after RegisterRoutes.

What I asked is do you use OnApplyRedirect as shown before?

Provider =
    new CookieAuthenticationProvider()
    {
        OnApplyRedirect = 
            context => context.Response.Redirect(
                url.Action(MVC.User.Login().AddRouteValues(new { culture = Thread.CurrentThread.CurrentCulture.Name })))
    }

If so, what URL is generated there? You have to use OnApplyRedirect because that is called for each Request and you can there switch to different Logins. If you only set the LoginPath, you can only redirect to this one specific URL.

I don't know why your URL is NULL there though.

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

This is a really strange issue as I confirmed that my Routes are being added before calling Owin.

Then on Owin setup I tried the following:

  UrlHelper url = new UrlHelper(HttpContext.Current.Request.RequestContext);

  // Testing routes > All returned null 
  var home = url.Action("home", "index");
  var homeT4MVC = url.Action(MVC.Home.Index());
  var login = url.Action("user", "login");   
  var loginT4MVC = url.Action(MVC.User.Login());

In url I confirmed there are 116 routes ... So at least the Home/Index should not return null.

I will try to replicate this in the sample project but this is quite strange.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

You can also upload your project to a github repository, and i will take a look then if thats easier for you.

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

Thanks. I will do that tomorrow. I will try to replicate the situation and upload it to Github.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

Any update on this issue?

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

If you run the project https://github.com/mdmoura/RouteLocalization.Test you will see two buttons.

One accesses tries to access an action with Authorize attribute. You will see a 403 error.

Something is wrong when using RouteLocalization and Owin ... Or maybe my configuration?

Please, see the RouteConfig and Startup.Auth files in App_Start ... Note the code line:

OnApplyRedirect = context => context.Response.Redirect(url.Action(MVC.Account.Login().AddRouteValues(new { culture = Thread.CurrentThread.CurrentCulture.Name })))

OnAppluRedirect works fine when not using RouteLocalization.

from routelocalization.

Dresel avatar Dresel commented on June 29, 2024

It seems when LoginPath isn't set at all, OnApplyRedirect is not called. Try to comment

// LoginPath = new PathString("/Account/Login")

in, then it should work.

from routelocalization.

mdmoura avatar mdmoura commented on June 29, 2024

Yes, that is right ... Solved.
I get back to this if I find any other problem.

from routelocalization.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.