在解决了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. 基于WebGL 的3D呈现A* Search Algorithm

    http://www.hightopo.com/demo/astar/astar.html 最近搞个游戏遇到最短路径的常规游戏问题,一时起兴基于HT for Web写了个A*算法的WebGL 3D呈现 ...

  2. JavaScript 随机数

    JavaScript内置函数random(seed)可以产生[0,1)之间的随机数,若想要生成其它范围的随机数该如何做呢? 生成任意范围的随机数 //生成[100,120)之间的随机数 Math.fl ...

  3. Cesium原理篇:5最长的一帧之影像

    如果把地球比做一个人,地形就相当于这个人的骨骼,而影像就相当于这个人的外表了.之前的几个系列,我们全面的介绍了Cesium的地形内容,详见: Cesium原理篇:1最长的一帧之渲染调度 Cesium原 ...

  4. Debian的软件包管理工具命令 (dpkg,apt-get)详解

    本文转载于:http://blog.chinaunix.net/uid-20769502-id-106056.html   1.dpkg包管理工具 dpkg --info "软件包名&quo ...

  5. shiro的使用2 灵活使用shiro的密码服务模块

    shiro最闪亮的四大特征是认证,授权,加密,会话管理. 上一篇已经演示了如何使用shiro的授权模块,有了shiro这个利器,可以以统一的编码方式对用户的登入,登出,认证进行管理,相当的优雅. 为了 ...

  6. Java泛型

    什么是泛型? 泛型(Generic type 或者 generics)是对 Java 语言的类型系统的一种扩展,以支持创建可以按类型进行参数化的类.可以把类型参数看作是使用参数化类型时指定的类型的一个 ...

  7. 灾难 bzoj 2815

    灾难(1s 128MB)catas [样例输入] 5 0 1 0 1 0 2 3 0 2 0 [样例输出] 4 1 0 0 0 题解: 主要算法:拓扑排序:最近公共祖先(Lca): 先跑出拓扑序 我们 ...

  8. 扩展JS Date对象时间格式化功能

    在自己JS代码中引入一下代码: Date.prototype.format =function(format) { var o = { "M+" : this.getMonth() ...

  9. java面试题——集合框架

    先来看一下集合框架关系图 Collection FrameWork 如下: Collection ├List │├LinkedList │├ArrayList │└Vector │ └Stack └S ...

  10. 《C#开发常用免费WebServices集合》

    天气预报 Web服务,数据来源于中国气象局 公用事业 http://www.webxml.com.cn/WebServices/WeatherWebService.asmx 中国股票行情 分时走势预览 ...