Cookie和ASP.NET核心 [英] Cookies and ASP.NET Core

查看:243
本文介绍了Cookie和ASP.NET核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,我希望至少是这样。

This might be a simple question, I'm hoping it is at least.

我已经开始研究ASP.NET核心的候选版本和我可以看到很多配置已从旧的web.config文件移出JSON结构文件(以及您可能想自己编写的XML和任何其他中间件)。

我还没有想到如何做的一件事是在旧的web.config方法中这么简单,保护你的网站的一些基本组件,如cookie。

I've started to look into the Release Candidate of ASP.NET Core and I can see that a lot of the configuration has been moved out of the old web.config file and into JSON structured files (as well as XML and any other middleware that you might want to write yourself).
The one thing I haven't yet figured out how to do is something that was so simple in the old web.config approach, securing some of the basic components of your site like cookies.

之前我们在web.config中设置了安全 httpOnly 等等,当它到来时部署一个漂亮的小变换文件会修改我们的值并在最后吐出新文件。在阅读了一下后,似乎web.config现在几乎已经死了,那么我们如何实现相同的结果呢?

Previously we'd set the secure, httpOnly and so on inside web.config and when it came to deployment a nice little transform file would modify the values for us and spit out the new file at the end. After reading round a bit, it seems that web.config is pretty much dead now, so how do we go about achieving the same results?

我知道我们可以加载不同的根据某些变量(例如环境)是否设置为DEV,STAGING,PRODUCTION等来配置文件,但这似乎只是将转换替换为所有意图和目的的转换,除了它实际加载的方式?

I know we can load different config files based on whether certain variables, such as environment, are set to DEV, STAGING, PRODUCTION etc. but this seems to be just replacing transforms with something that is a transform for all intents and purposes except in how it's actually loaded?

我在这里错过了什么或者我是否设法让自己变得有点乱?

Have I missed something here or have I managed to work myself into a bit of a mess?

推荐答案

对于在应用程序中手动创建的常规cookie,您可以在创建时控制安全标志 - 例如:

For a general cookie manually created within your application, you control the flags for security when creating it - for example:

Response.Cookies.Append(
    "COOKIE_NAME",
    "COOKIE_VALUE",
    new CookieOptions()
    {
        Path = "/",
        HttpOnly = false,
        Secure = false
    }
);

这里,将HttpOnly设置为true会阻止客户端JS访问cookie vlaue,并设置Secure为true只允许通过HTTPS提供/接收cookie。

Here, setting HttpOnly to true would prevent client-side JS from accessing the cookie vlaue, and setting Secure to true would only allow the cookie to be served/received over HTTPS.

向响应添加cookie时不会应用默认值,如 ResponseCookies类的源代码

No defaults are applied when you add cookies to the response, as can be seen in the source code for the ResponseCookies class.

对于创建和使用自己的cookie的各种中间件(比如你在答案中提到的Session中间件),他们可能有自己的配置选项来控制这些他们自己创建的cookie的标志,但这对您在应用程序中其他地方创建的cookie没有任何影响。

For the various middlewares that create and consume their own cookies (like the Session middleware that you have mentioned in your answer), they may have their own configuration options that will control these flags for those cookies they create themselves, but this will make no difference to cookies you create elsewhere in your application.

这篇关于Cookie和ASP.NET核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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