我正在更改身份验证实现,以将MVC5 ASP.NET Identity与Owin结合使用。
但是,我们需要将登录与同一域中的其他链接的应用程序和网站集成在一起。目前,我们是通过跨多个子域的应用程序之间共享cookie来实现的。各种应用程序和技术(即,并非全部都在.NET或同一台服务器上)可以使用的一种特定格式和加密算法的cookie。
我发现在App_Start ConfigureAuth.cs中,您可以设置app.UseCookieAuthentication来指定Cookie名称和Cookie的子域(例如,跨子域的ASP.NET Identity Cookie)之类的内容。
这是一个很好的开始,但是我还需要将cookie的实际值更改为特定的格式和加密算法。
有谁知道如何自定义用于创建和读取Cookie的值和加密类型?
谢谢您的帮助,Saan
CookieAuthenticationOptions类具有一个名为TicketDataFormat的属性,用于此目的。您可以实现自定义ISecureDataFormat对象并实现此目的。如果尚未覆盖此票证数据格式,则会为其指定默认值。
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);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句