ASP.NET MVC, set culture after authorization

旧城冷巷雨未停 提交于 2019-12-12 21:45:28

问题


We want to set the Thread.CurrentCulture

  • before the ASP.NET MVC model binder / validator runs (of course), but
  • after the user has been authorized (as we want to load the culture from a UserSettings table)

What's the correct extension point we should go for?

An action filter is too late, Global.asax is too early (user not authorized yet). Anyone with a good idea?


回答1:


Global.asax is too early

Global.asax has events that occur at various points in the request life cycle, some too early (BeginRequest), some too late (EndRequest), and maybe one that is just right for your requirement.

Maybe a handler for PostAuthorizeRequest, or if you want to store stuff in Session, PostAcquireRequestState would do?




回答2:


You could use the AuthorizationFilter and just extend the build in functionality. At the point of authorization you will be able to tell whether they are authorized or not and perform your logic accordingly.

public class CustomAuthAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        bool isAuthorized = base.AuthorizeCore(httpContext);

        if (isAuthorized)
        {
            // set culture if user is authorized
        }
        else
        {
            // set culture if user is not authorized
        }

        return isAuthorized;
    }
}


来源:https://stackoverflow.com/questions/18701065/asp-net-mvc-set-culture-after-authorization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!