gpt4 book ai didi

c#-4.0 - ASP.NET MVC 4 cookie 消失

转载 作者:行者123 更新时间:2023-12-02 00:15:38 29 4
gpt4 key购买 nike

我有一个 ASP.NET 应用程序将身份验证 cookie 发送到 ASP.NET MVC 应用程序,用作后台应用程序。

我添加了一个全局过滤器,用于检查身份验证 cookie 的每个 Controller 操作。如果 cookie 存在,它允许用户进入页面。

代码如下所示:

 public class SecurityFilter : FilterAttribute, IAuthorizationFilter
{
public void OnAuthorization(AuthorizationContext filterContext)
{
// TODO: For some reason .AUTHCookie cookie isn't exist in request context of filter,

HttpCookie cookie = filterContext.RequestContext.HttpContext.Request.Cookies[".AUTHCookie "];


if (cookie != null) {

从另一边,我可以在 Global.asax 文件的 Application_BeginRequest 事件中看到从 ASP.NET 应用程序发送的 cookie。

cookie 在哪里以及为什么消失了?在 MVC 请求处理管道的哪个部分丢弃了 cookie?

  protected void Application_BeginRequest(object sender, EventArgs e)
{
var cookies = HttpContext.Current.Request.Cookies;
// HERE I CAN SEE BOTH cookies. In filter action only one cookie was found. The authentication cookie is thrown somewhere ...
}

最佳答案

我找到了适合我的场景的解决方案。我在两个应用程序的 machinekey 中都添加了 compatibilityMode="Framework45",一切正常。

注意:如果您的某个应用程序正在使用较旧版本的 .NET 框架,您必须明确配置您的 .NET 4.5 应用程序以使用较早的机器兼容模式,否则它们将无法加密/解密表单例份验证票。

只是提醒你我的场景:

WebForms ASP.NET 4.5

<machineKey compatibilityMode="Framework45" decryption="AES" validation="SHA1" decryptionKey="your_key1" validationKey="your_keu2" />
<authentication mode="Forms">
<forms name="_authcookie" domain=".domain.com" loginUrl="Default.aspx?View=1" defaultUrl="Default.aspx?View=1" timeout="30" path="/" protection="All" slidingExpiration="true" enableCrossAppRedirects="true" />
</authentication>

MVC 4
<machineKey compatibilityMode="Framework45" decryption="AES" validation="SHA1" decryptionKey="your_key1" validationKey="your_keu2" />
<authentication mode="Forms">
<forms name="_authcookie" domain=".domain.com" defaultUrl="~/" timeout="30" path="/" protection="All" slidingExpiration="true" enableCrossAppRedirects="true" />
</authentication>

兼容模式的可能值:

http://msdn.microsoft.com/en-us/library/system.web.configuration.machinekeysection.compatibilitymode.aspx

关于c#-4.0 - ASP.NET MVC 4 cookie 消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13343073/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com