Asp.Net MVC5如何确保cookie存在? [英] Asp.Net MVC5 How to ensure that a cookie exists?

查看:364
本文介绍了Asp.Net MVC5如何确保cookie存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC的新手(5).为了向我的网站添加本地化支持,我在ApplicationUser : IdentityUser

中添加了一个"语言"字段

现在将这些信息存储在浏览器中并确保重新创建该信息的最佳方法是什么,即使用户手动将其删除?


TL;但我有时间

到目前为止我一直尝试的操作:

我开始在方法private async Task SignInAsync(ApplicationUser user, bool isPersistent)中创建 cookie ,但我注意到:

  1. 如果用户已经通过身份验证并使用 .Aspnet.Applicationcookie 自动登录,并且我的语言Cookie可能同时过期(或已删除),则不使用此方法.

  2. p>
  3. 用户可以手动删除cookie,只是为了好玩.

我考虑过检查它在控制器中的存在(查询登录的用户并从数据库中获取它),它可以工作,但我需要在每个控制器中都进行检查.我不确定执行此操作的正确方法.

关于如何解决此问题并确保应用程序在每个请求上都有有效的语言cookie"的任何建议?

解决方案

在我看来,您想要的是

然后在全局范围内注册您的属性,以使其成为每个控制器操作的默认行为

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new EnsureLanguagePreferenceAttribute());
}

I'm new to MVC (5). In order to add localization support to my website I added a "Language" field to my ApplicationUser : IdentityUser

What's the best approach to now store this information in the browser and ensure that it gets re-created even if the user manually deletes it?


TL; but I've got time

What I've tried until now:

I started creating a cookie in my method private async Task SignInAsync(ApplicationUser user, bool isPersistent) but I notice that:

  1. This method is not used if the user is already authenticated and automatically logs in using the .Aspnet.Applicationcookie and my language cookie could be meanwhile expired (or been deleted).

  2. A user could manually delete the cookie, just for fun.

I thought about checking its existence in the controller (querying the logged user and getting it from the db) and it works but I'd need to do it in EVERY controller. I'm not sure is the correct way to do this.

Any suggestion about how to approach this problem and guarantee that the application has a valid "language cookie" on every request?

解决方案

It sounds to me like what you want here is a Custom Action Filter. You can override the OnActionExecuting method which means the logic is run before any action is called

public class EnsureLanguagePreferenceAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var langCookie = filterContext.HttpContext.Request.Cookies["LanguagePref"];
        if (langCookie == null)
        {
            // cookie doesn't exist, either pull preferred lang from user profile
            // or just setup a cookie with the default language
            langCookie = new HttpCookie("LanguagePref", "en-gb");
            filterContext.HttpContext.Request.Cookies.Add(langCookie);
        }
        // do something with langCookie
        base.OnActionExecuting(filterContext);
    }
}

Then register your attribute globally so it just becomes the default behaviour on every controller action

public static void RegisterGlobalFilters(GlobalFilterCollection filters)
{
    filters.Add(new HandleErrorAttribute());
    filters.Add(new EnsureLanguagePreferenceAttribute());
}

这篇关于Asp.Net MVC5如何确保cookie存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆