c# – 自定义MVC5 ASP.NET标识中的cookie值
发布时间:2020-12-15 08:21:32 所属栏目:百科 来源:网络整理
导读:我正在更改我们的身份验证实现以使用Owin的MVC5 ASP.NET身份. 但是,我们需要将我们的登录与同一域中的其他链接应用程序和网站集成.我们目前通过在多个子域之间共享应用程序之间的cookie来实现此目的. Cookie采用非常特定的格式和加密算法,可以使用各种应用程
我正在更改我们的身份验证实现以使用Owin的MVC5 ASP.NET身份.
但是,我们需要将我们的登录与同一域中的其他链接应用程序和网站集成.我们目前通过在多个子域之间共享应用程序之间的cookie来实现此目的. Cookie采用非常特定的格式和加密算法,可以使用各种应用程序和技术(即并非所有应用程序和技术都在.NET或同一服务器上). 我发现在App_Start ConfigureAuth.cs中你可以设置app.UseCookieAuthentication来指定cookie名称和cookie子域的内容(例如ASP.NET Identity Cookie across subdomains). 这是一个非常好的开始,但我还需要将cookie的实际值更改为特定格式和加密算法. 有谁知道如何自定义用于创建和读取cookie的值和加密类型? 谢谢你的帮助, 解决方法
CookieAuthenticationOptions类有一个名为TicketDataFormat的属性,用于此目的.您可以实现自定义的ISecureDataFormat对象并实现此目的.如果您没有覆盖此TicketDataFormat,则已为此TicketDataFormat指定了默认值.
app.UseCookieAuthentication(new CookieAuthenticationOptions() { TicketDataFormat = new MyCustomSecureDataFormat() }); public class MyCustomSecureDataFormat : ISecureDataFormat<AuthenticationTicket> { private static AuthenticationTicket savedTicket; public string Protect(AuthenticationTicket ticket) { //Ticket value serialized here will be the cookie sent. Encryption stage. //Make any changes if you wish to the ticket ticket.Identity.AddClaim(new Claim("Myprotectionmethod","true")); return MySerializeAndEncryptedStringMethod(ticket); } public AuthenticationTicket Unprotect(string cookieValue) { //Invoked everytime when a cookie string is being converted to a AuthenticationTicket. return MyDecryptAndDeserializeStringMethod(cookieValue); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |