MVC AspNet-在同一服务器上的2个应用程序之间共享FormsAuthentication Cookie

时间:2019-03-13 14:16:35

标签: cookies model-view-controller forms-authentication

我在SAME服务器(Windows Server 2016,IIS 10)上都具有frameWork 4.6.1和 application_A application_B

application_A 中,使用用户名和psw登录: 在 application_A 的控制器中,我使用:FormsAuthentication.SetAuthCookie(username, false);

application_A 的web.config中,有:

<authentication mode="Forms">
  <forms loginUrl="/Utente/login" enableCrossAppRedirects="true" domain="192.168.20.1"  cookieless="UseCookies" timeout="2880" />
</authentication>

从登录的用户 application_A 中,需要通过html链接转到 application_B 所以我需要在 application_B 中检查写在cookie中的用户名,所以我使用以下代码:

string cookieName = FormsAuthentication.FormsCookieName; 
HttpCookie cookie = HttpContext.Request.Cookies[cookieName]; 
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(cookie.Value); 
string UserName = ticket.Name; 

application_B 的web.config中,有:

<authentication mode="Forms">
  <forms loginUrl="~/Home/Index" enableCrossAppRedirects="true" domain="192.168.20.1"   cookieless="UseCookies"  timeout="2880"/>
</authentication>

问题是,当我从 application_A 到达 application_B 时,控制器的ActionResult Index给了我这个错误:

Server Error in '/application_B' Application.

Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. 

Stack Trace: 

[NullReferenceException: Object reference not set to an instance of an object.]
….

application_B 行中:

string cookieName = FormsAuthentication.FormsCookieName; 

在本地主机中,所有应用程序都可以正常运行,但是当我在服务器上发布时, application_B 会给我错误。

有什么想法吗? 谢谢

0 个答案:

没有答案