如何为解决方案中的每个应用程序创建独特的FedAuth cookie?

时间:2016-07-12 11:17:31

标签: asp.net-mvc authentication cookies wif

我正在使用System.IdentityModel和基于声明的身份验证来验证我的MVC应用。 在检查用户名和密码后的登录页面中,我使用这种方法创建FedAuth Cookie:

   var token = new SessionSecurityToken(MyCustomClaimsPrincipal.GetInstance(username, roles,claims)            );       
   token.IsPersistent=rememberMe;

   var sam = FederatedAuthentication.SessionAuthenticationModule;
   sam.IsReferenceMode = true;
   sam.WriteSessionTokenToCookie(token);

一切正常但问题是当我在同一解决方案中的其他2个应用程序中使用完全相同的方法时,将在不同的域上发布;但是在localhost中FedAuth Cookies相互冲突。当我登录到一个应用程序时,cookie在其他应用程序中无效,我应该再次登录。

事实上,我只能同时使用一个应用程序。 我认为它只在localhost上,发布后在不同的域上没有问题。但我更倾向于在一个解决方案中为每个应用程序创建独特的FedAuth cookie。可能吗?如何? 也许在SessionSequrityToken的构造函数中使用Context或ContextId是可能的。但我没有找到任何关于这方面的例子。我不知道要发送什么作为context和contextId params?

1 个答案:

答案 0 :(得分:1)

sessionAuthenticationModule有一个名为cookiehandler(https://msdn.microsoft.com/en-us/library/microsoft.identitymodel.web.sessionauthenticationmodule_members.aspx的成员)该cookiehandler的名称是cookie的名称。 https://msdn.microsoft.com/en-us/library/microsoft.identitymodel.web.cookiehandler_members.aspx

你也可以在配置中做你正在做的事情 <system.identityModel.services> <federationConfiguration> <cookieHandler requireSsl="false" name="yourcookienamehere"/> ...

</system.identityModel.services>

希望这有帮助