asp.net-mvc – Owin如何在Application_EndRequest阶段之后设置A
作为测试,我使用Visual Studio 2013中的最新模板创建了一个全新的Asp.Net MVC5应用程序.我将以下方法添加到Global.asax.cs:
protected void Application_PreSendRequestHeaders() { Response.AppendCookie(new HttpCookie("TotalNumberOfCookiesInApplication_EndRequestIs",Response.Cookies.Count + string.Empty)); } 当我启动应用程序并使用注册用户的凭据对/ Account / Login执行POST时,返回给客户端的cookie是: 请注意,我添加的自定义cookie显示,在调用Application_PreSendRequestHeaders()时,响应中没有设置cookie.尽管如此,所有Auth cookie都会到达客户端.我理解Application_PreSendRequestHeaders()是我们可以“挂钩”修改cookie的最后阶段. Owin中间件是否能够以某种方式添加cookie,或者我错过了什么? (如果你感兴趣的话,我对这一切的动机是:我试图将auth cookie的域名修改为“.abc.com”,其中“abc.com”是主机的最后两部分在请求URI中.我想这样做是为了支持跨多个子域的身份验证.在全局Owin配置(IAppBuilder)的上下文中设置CookieDomain是不够的,因为请求主机在我们的调试/登台/生产环境之间发生变化,在进行VIP交换之前,我们经常首先将生产代码部署到Azure分段进行测试. (另请注意,我知道像this one这样的帖子,但是它没有解释实际设置cookie的位置) 编辑: 基于更多的搜索,似乎我正在寻找错误的管道. Owin有自己的管道,所以我找到了this post,它描述了我们如何能够加入它. Viola …有饼干.如果有人能够确认这确实是最明智的做法,那将会很棒. 编辑2: 最后决定查看Katana源代码并发现我需要做的就是设置我的cookie域名是我的CookieAuthenticationProvider中的以下代码 OnResponseSignIn = context => { // Example only! context.CookieOptions.Domain = context.Request.Uri.Host; },OnResponseSignOut = context => { // Example only! context.CookieOptions.Domain = context.Request.Uri.Host; } 编辑3: 对我来说更简洁的解决方案就是使用自定义cookie管理器,它根据当前请求URI设置cookie域: /// <summary> /// This class simply appends the cookie domain to the usual auth cookies /// </summary> public class ChunkingCookieManagerWithSubdomains : ICookieManager { private readonly ChunkingCookieManager _chunkingCookieManager; public ChunkingCookieManagerWithSubdomains() { _chunkingCookieManager = new ChunkingCookieManager(); } public string GetRequestCookie(IOwinContext context,string key) { return _chunkingCookieManager.GetRequestCookie(context,key); } public void AppendResponseCookie(IOwinContext context,string key,string value,CookieOptions options) { // Simplification (use the context parameter to get the required request info) options.Domain = ".domainBasedOnRequestInContext.com"; _chunkingCookieManager.AppendResponseCookie(context,key,value,options); } public void DeleteCookie(IOwinContext context,CookieOptions options) { // Simplification (use the context parameter to get the required request info) options.Domain = ".domainBasedOnRequestInContext.com"; _chunkingCookieManager.DeleteCookie(context,options); } } …然后在Owin设置中的Cookie身份验证选项中设置: app.UseCookieAuthentication(new CookieAuthenticationOptions { ... CookieManager = new ChunkingCookieManagerWithSubdomains(),... } }); 希望能帮助有人遇到同样的问题. 解决方法
根据Tieson的要求,这里是我在上面原始帖子中编辑的摘要,作为答案.
建议的解决方案:使用自定义cookie管理器. /// <summary> /// This class simply appends the cookie domain to the usual auth cookies /// </summary> public class ChunkingCookieManagerWithSubdomains : ICookieManager { private readonly ChunkingCookieManager _chunkingCookieManager; public ChunkingCookieManagerWithSubdomains() { _chunkingCookieManager = new ChunkingCookieManager(); } public string GetRequestCookie(IOwinContext context,options); } } …然后可以在Owin设置中的Cookie身份验证选项中设置: app.UseCookieAuthentication(new CookieAuthenticationOptions { ... CookieManager = new ChunkingCookieManagerWithSubdomains(),... } }); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- asp.net – NodaTime转换(第2部分).如何?
- asp.net – 如何强制实体框架插入标识列?
- asp.net – 是否有使用Markdown作为编辑器的WIKI(最好是.NE
- CLR调试器,ASP.NET – 如何增加超时?
- asp.net – Visual Studio总是打开网站而不是编辑的网页
- asp.net-mvc – ASP.NET MVC在HTTP DELETE请求上有405错误?
- ASP.NET MVC默认项目结构和脚本文件夹
- 高级ASP.NET主机
- asp.net-mvc – 在.aspx上获得MVC Razor青睐.cshtml
- asp.net – Html.Partial()跳过控制器动作
- 围绕ASP.NET会话跨多个选项卡窗口共享的方式
- asp.net – Microsoft.Office.Interop.Excel在64
- ASP.NET:Response.Redirect(…)后的代码会发生什
- asp.net – bing地图花钱?
- asp.net – 添加context.Response.Headers.Add(“
- asp.net-mvc – Asp.NET MVC 2预览2:Area的aspx
- asp.net-mvc – 在ASP.Net MVC中定义自定义URL路
- asp.net – 使用jquery调用ascx页面方法
- asp.net mvc datatable
- asp.net – Visual Basic以编程方式将用户名和密