在本地IIS上跨子域进行表单身份验证

Par*_*der 6 c# subdomain asp.net-mvc forms-authentication

我知道可以使用该设置在多个子域中共享cookie

<forms 
    name=".ASPXAUTH" 
    loginUrl="Login/" 
    protection="Validation" 
    timeout="120" 
    path="/"     
    domain=".mydomain.com"/>
Run Code Online (Sandbox Code Playgroud)

在Web.config中.但是如何在本地机器上复制相同的东西.我在笔记本电脑上使用Windows 7和IIS 7.所以我的网站localhost.users /为我的实际网站users.mysite.com localhost.host/为host.mysite.com和类似.

Dar*_*rov 9

localhost.users并且localhost.host是跨域的.Cookie不能跨域共享.

您可以像这样配置它,以便子域不同但根域保持不变:

  • users.localhost
  • host.localhost

现在将web.config中的cookie域设置为localhost:

domain=".localhost"
Run Code Online (Sandbox Code Playgroud)

并在您的c:\Windows\System32\drivers\etc\hosts文件中添加以下2个条目:

127.0.0.1 users.localhost
127.0.0.1 host.localhost
Run Code Online (Sandbox Code Playgroud)

现在,您将能够在users.localhost和之间成功共享身份验证cookie host.localhost.

啊,不要忘记在自动构建过程中加入一步,将web.config值转换为正确的根域,然后再在生产中发货.