【问题标题】:Namespace for Response.Cookies.Append() in ASP.NET MVCASP.NET MVC 中 Response.Cookies.Append() 的命名空间
【发布时间】:2017-08-15 20:07:47
【问题描述】:

我需要使用以下代码来设置会话 cookie 的到期日期

Response.Cookies.Append(
        OpenIdConnectAuthenticationDefaults.CookieNoncePrefix + Options.StringDataFormat.Protect(nonce),
        NonceProperty,
        new CookieOptions
        {
            HttpOnly = true,
            Secure = Request.IsHttps,
            Expires = DateTime.UtcNow + Options.ProtocolValidator.NonceLifetime
        });

有人可以帮我找出 Response.Cookies.Append() 的命名空间吗?我尝试使用命名空间 System.Web 和 System.Web.HTTP 。

我从下面的链接得到了上面的代码

http://katanaproject.codeplex.com/workitem/402

提前致谢

饭菜

【问题讨论】:

    标签: c# asp.net-mvc cookies openid-connect


    【解决方案1】:

    命名空间:System.Web.Mvc

    响应将是您的控制器中存在的属性。

    public HttpResponseBase Response { get; }
    

    【讨论】:

    • 添加了 System.Web.Mvc 和我在控制器中的代码,但仍然面临问题。以下是我在控制器中使用的命名空间'using System.Web;使用 System.Web.Mvc;使用 Microsoft.Owin.Security;使用 Microsoft.Owin.Security.Cookies;使用 Microsoft.Owin.Security.OpenIdConnect;'
    【解决方案2】:

    它实际上是 ControllerBase 的一部分(这是 Controller 继承的。因此,如果您将代码放在控制器中的 action 方法中,它将起作用。

    public class HomeController : Controller
    {
        [HttpPost]
        public IActionResult MyActionMethod()
        {
            Response.Cookies.Append(
            OpenIdConnectAuthenticationDefaults.CookieNoncePrefix + Options.StringDataFormat.Protect(nonce),
            NonceProperty,
            new CookieOptions
            {
                HttpOnly = true,
                Secure = Request.IsHttps,
                Expires = DateTime.UtcNow + Options.ProtocolValidator.NonceLifetime
            });
    
            return View();
        }
    }
    

    【讨论】:

    • 在控制器中以操作方法添加了我的代码,但仍然是同样的问题。
    • 以下是我在控制器中使用 System.Web 的命名空间;使用 System.Web.Mvc;使用 Microsoft.Owin.Security;使用 Microsoft.Owin.Security.Cookies;使用 Microsoft.Owin.Security.OpenIdConnect;'
    • 您看到的确切错误信息是什么?
    猜你喜欢
    • 2011-07-17
    • 2010-11-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-13
    • 1970-01-01
    • 1970-01-01
    • 2010-11-18
    相关资源
    最近更新 更多