在解决了asp.net core中访问memcached缓存的问题后,我们开始大踏步地向.net core进军——将更多站点向asp.net core迁移,在迁移涉及获取用户登录信息的站点时,我们遇到了一个问题——如何在asp.net core与传统asp.net之间共享保存用户登录信息的cookie?

对于cookie的加解密,传统asp.net用的是对称加解密算法,而asp.net core用的是基于公钥私钥的非对称加解密算法,所以asp.net core无法解密传统asp.net生成的cookie,传统asp.net也无法解密asp.net core生成的cookie。针对于这个问题,.net社区已经有人提供了解决办法——让传统asp.net改用和asp.net core一样的加解密算法(详见这里),但是这需要修改所有涉及获取用户登录信息的传统asp.net站点的代码,有些奢侈,不到万不得已,我们不想采用,我们要另辟蹊径。

先简化一下问题,根据我们向ASP.NET Core迁移过渡阶段的实际场景,用户登录操作是在传统asp.net站点上完成的,我们只需在asp.net core站点中解密cookie获取用户登录信息即可,连加密都不需要。既然asp.net core自己解密不了,那可以让传统asp.net帮忙解密,asp.net core将接收到的cookie通过web api发给传统asp.net解密。简化后问题变成了——在asp.net core中如何接收传统asp.net的cookie?如何拦截asp.net core对cookie的解密操作?传统asp.net如何在web api中从cookie中解密用户验证信息(FormsAuthenticationTicket)?在asp.net core中如何将FormsAuthenticationTicket转换为自身的验证信息(AuthenticationTicket)?我们来逐一解决这些问题。

问题一:在asp.net core中如何接收传统asp.net的cookie?

这个问题很容易解决。只需在Startup.cs中将CookieAuthenticationOptions的CookieName与CookieDomain设置为与传统asp.net一样。

var cookieOptions = new CookieAuthenticationOptions
{
CookieName = ".CnblogsCookie",
CookieDomain = ".cnblogs.com",
};
app.UseCookieAuthentication(cookieOptions);

问题二:如何拦截asp.net core对cookie的解密操作?

这个问题比较棘手。我们是通过阅读 Microsoft.AspNetCore.Authentication.Cookies 的源码在 CookieAuthenticationHandler.cs 中将 TicketDataFormat 揪了出来:

private async Task<AuthenticateResult> ReadCookieTicket()
{
var cookie = Options.CookieManager.GetRequestCookie(Context, Options.CookieName);
//...
var ticket = Options.TicketDataFormat.Unprotect(cookie, GetTlsTokenBinding());
//...
return AuthenticateResult.Success(ticket);
}

TicketDataFormat的类型是ISecureDataFormat<AuthenticationTicket>接口,解密cookie就是调用这个接口的Unprotect方法:

public interface ISecureDataFormat<TData>
{
string Protect(TData data);
string Protect(TData data, string purpose);
TData Unprotect(string protectedText);
TData Unprotect(string protectedText, string purpose);
}

而且TicketDataFormat是CookieAuthenticationOptions的一个属性,我们可以直接修改这个属性值,使用自己的ISecureDataFormat接口实现(默认实现是SecureDataFormat),在Unprotect()方法的实现中读取protectedText参数值(这个应该就是接收到的cookie值)达到拦截目的,我们试一下。

定义一个 FormsAuthTicketDataFormat 类,实现 ISecureDataFormat<AuthenticationTicket> 接口:

public class FormsAuthTicketDataFormat : ISecureDataFormat<AuthenticationTicket>
{
//... public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
Console.WriteLine($"{nameof(Unprotect)}(\"{protectedText}\", \"{purpose}\")");
throw new NotImplementedException();
}
}

在Startup.cs中应用FormsAuthTicketDataFormat:

var cookieOptions = new CookieAuthenticationOptions
{
//...
TicketDataFormat = new FormsAuthTicketDataFormat()
};
app.UseCookieAuthentication(cookieOptions);

经测试验证,接收到的的确是传统asp.net生成的cookie值。

既然在Unprotect()方法中已经能读取到cookie值,那紧接着就可以将它通过web api发送给传统asp.net解密,于是进入下一个问题。

问题三:传统asp.net如何在web api中从cookie中解密用户验证信息(FormsAuthenticationTicket)?

这个问题也很好解决,只需调用 FormsAuthentication.Decrypt() 方法进行解密,并将FormsAuthenticationTicket的Name, IssueDate, Expiration三个值返回给asp.net core。

public IHttpActionResult GetTicket(string cookie)
{
var formsAuthTicket = FormsAuthentication.Decrypt(cookie);
return Ok(new
{
formsAuthTicket.Name,
formsAuthTicket.IssueDate,
formsAuthTicket.Expiration
});
}

asp.net core中通过调用web api解密cookie并得到Name, IssueDate, Expiration这三个值,于是FormsAuthTicketDataFormat.Unprotect()的实现代码变成了这样:

public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
var formsAuthTicket = GetFormsAuthTicket(protectedText);
var name = formsAuthTicket.Name;
DateTime issueDate = formsAuthTicket.IssueDate;
DateTime expiration = formsAuthTicket.Expiration; throw new NotImplementedException();
}

接下来解决最后一个问题。

问题四:在asp.net core中如何将FormsAuthenticationTicket转换为自身的验证信息(AuthenticationTicket)?

由于Unprotect()方法返回参数的类型就是AuthenticationTicket,所以我们不用换地方,继续在这个方法中折腾。现在我们已经有了FormsAuthenticationTicket的三个值Name, IssueDate, Expiration,我们需要基于它们创建有效的AuthenticationTicket。

AuthenticationTicket的构造函数有3个参数,第1个参数的类型是ClaimsPrincipal,与用户名相关联;第2个参数的类型是AuthenticationProperties,cookie的生成时间与过期时间就存储于其中,第3个参数authenticationScheme设置为对应的值(这里设置为空字符串),代码如下:

public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
//Get FormsAuthenticationTicket from asp.net web api
var formsAuthTicket = GetFormsAuthTicket(protectedText);
var name = formsAuthTicket.Name;
DateTime issueDate = formsAuthTicket.IssueDate;
DateTime expiration = formsAuthTicket.Expiration; //Create AuthenticationTicket
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, name) }, "Basic");
var claimsPrincipal = new System.Security.Claims.ClaimsPrincipal(claimsIdentity);
var authProperties = new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties
{
IssuedUtc = issueDate,
ExpiresUtc = expiration
};
var ticket = new AuthenticationTicket(claimsPrincipal, authProperties, _authenticationScheme);
return ticket;
}

解决这4个问题后就大功告成了!在aps.net core mvc controller中就能显示当前登录用户名,比如下面的代码:

public IActionResult Index()
{
return Content(User.Identity.Name);
}

完整相关实现代码如下:

Startup.cs

public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
var cookieOptions = new CookieAuthenticationOptions
{
AutomaticAuthenticate = true,
AutomaticChallenge = true,
CookieHttpOnly = true,
CookieName = ".CnblogsCookie",
CookieDomain = ".cnblogs.com",
LoginPath = "/account/signin",
TicketDataFormat = new FormsAuthTicketDataFormat("")
};
app.UseCookieAuthentication(cookieOptions); //...
}

FormAuthTicketDataFormat.cs

public class FormsAuthTicketDataFormat : ISecureDataFormat<AuthenticationTicket>
{
private string _authenticationScheme; public FormsAuthTicketDataFormat(string authenticationScheme)
{
_authenticationScheme = authenticationScheme;
} public AuthenticationTicket Unprotect(string protectedText, string purpose)
{
//Get FormsAuthenticationTicket from asp.net web api
var formsAuthTicket = GetFormsAuthTicket(protectedText);
var name = formsAuthTicket.Name;
DateTime issueDate = formsAuthTicket.IssueDate;
DateTime expiration = formsAuthTicket.Expiration; //Create AuthenticationTicket
var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, name) }, "Basic");
var claimsPrincipal = new System.Security.Claims.ClaimsPrincipal(claimsIdentity);
var authProperties = new Microsoft.AspNetCore.Http.Authentication.AuthenticationProperties
{
IssuedUtc = issueDate,
ExpiresUtc = expiration
};
var ticket = new AuthenticationTicket(claimsPrincipal, authProperties, _authenticationScheme);
return ticket;
} public string Protect(AuthenticationTicket data)
{
throw new NotImplementedException();
} public string Protect(AuthenticationTicket data, string purpose)
{
throw new NotImplementedException();
} public AuthenticationTicket Unprotect(string protectedText)
{
throw new NotImplementedException();
} private FormsAuthTicketDto GetFormsAuthTicket(string cookie)
{
return new UserService().DecryptCookie(cookie).Result;
}
}

遗留问题:目前对[Authorize]标记不起作用。

更新:

遗留问题已解决,将

var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, name) });

改为

var claimsIdentity = new ClaimsIdentity(new Claim[] { new Claim(ClaimTypes.Name, name) }, "Basic");

也就是将authenticationType的值设为"Basic"。

.NET跨平台之旅:ASP.NET Core从传统ASP.NET的Cookie中读取用户登录信息的更多相关文章

  1. Asp.Net Core 项目实战之权限管理系统(5) 用户登录

    0 Asp.Net Core 项目实战之权限管理系统(0) 无中生有 1 Asp.Net Core 项目实战之权限管理系统(1) 使用AdminLTE搭建前端 2 Asp.Net Core 项目实战之 ...

  2. ASP.NET Core: 全新的ASP.NET !

    背景 最新版本的 ASP.NET 叫做 ASP.NET Core (也被称为 ASP.NET 5)   它颠覆了过去的 ASP.NET. 什么是 ASP.NET Core? ASP.NET Core ...

  3. ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 基础教程 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 是对 ASP.NET 有重大意义的一次重新设计.本章节我们将介绍 A ...

  4. ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 配置 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 配置 上一章节我们简单介绍了下 Id ...

  5. ASP.NET Core Identity 框架 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core Identity 框架 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core Identity 框架 前面我们使用了 N 多个章节, ...

  6. ASP.NET Core 数据库上下文 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 数据库上下文 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 数据库上下文 上一章节中我们了解了 Entity Framewo ...

  7. ASP.NET Core 动作结果 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 动作结果 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 动作结果 前面的章节中,我们一直使用简单的 C# 类作为控制器. 虽 ...

  8. ASP.NET Core 属性路由 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core 属性路由 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core 属性路由 经过前面章节的学习,想必你已经对 ASP.NET Core ...

  9. ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程

    原文:ASP.NET Core MVC 设计模式 - ASP.NET Core 基础教程 - 简单教程,简单编程 ASP.NET Core MVC 设计模式 上一章节中,我们提到 ASP.NET Co ...

随机推荐

  1. 『.NET Core CLI工具文档』(十三)dotnet-publish

    说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-publish 翻译:dotnet-publish 名称 dotnet-publish - 打包应用程序及 ...

  2. WebUtils-网络请求工具类

    网络请求工具类,大幅代码借鉴aplipay. using System; using System.Collections.Generic; using System.IO; using System ...

  3. [Tool] Open Live Writer插件开发

    一 前言 Windows Live Writer(简称 WLW)开源之后变成 Open Live Writer(简称 OLW),原先 WLW 的插件在 OLW 下都不能用了,原因很简单,WLW 插件开 ...

  4. 随机数(random)

    需求 Random rd=new Random(); 需要十以内的随机数  (0---10) System.out.println((int)((rd.nextDouble()*100)/10)); ...

  5. Linux Cmd Tool 系列之—alias

    The alias cmd list your current aliases. For example : alias Use alias to shorten a long cmd in curr ...

  6. [deviceone开发]-底部弹出选择

    一.简介 个人上传的第一个示例源码,两天空闲时间写的,一点简单组件,写的挺乱还没啥注释,仅供新手学习. 底部弹出选择,可滑动选择选项,如果停留在选项中间,可自动校正位置,加了一点简单的动画效果,需要的 ...

  7. 很强大的HTML+CSS+JS面试题(附带答案)

    一.单项选择(165题) 1.HTML是什么意思? A)高级文本语言 B)超文本标记语言 C)扩展标记语言 D)图形化标记语言 2.浏览器针对于HTML文档起到了什么作用? A)浏览器用于创建HTML ...

  8. 【前端构建】WebPack实例与前端性能优化

    计划把微信的文章也搬一份上来. 这篇主要介绍一下我在玩Webpack过程中的心得.通过实例介绍WebPack的安装,插件使用及加载策略.感受构建工具给前端优化工作带来的便利. 壹 | Fisrt 曾几 ...

  9. 八皇后算法的另一种实现(c#版本)

    八皇后: 八皇后问题,是一个古老而著名的问题,是回溯算法的典型案例.该问题是国际西洋棋棋手马克斯·贝瑟尔于1848年提出:在8×8格的国际象棋上摆放八个皇后,使其不能互相攻击,即任意两个皇后都不能处于 ...

  10. CentOS7上安装QGIS

    1.加入YUM源 rpm -Uvh http://download.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-8.noarch.rpm ...