让用户登录子域名asp.net

时间:2015-07-23 16:37:22

标签: asp.net asp.net-mvc subdomain

目前我正在使用Visual Studio 2013中的localhost。(我正在使用个人用户身份验证)

我所做的是:

我将<system.web>中的Web.config更新为

<system.web>
    <authentication mode="Forms">
        <forms loginUrl="~/Account/Login" timeout="2880" name=".ASPXAUTH" protection="Validation" path="/" domain=".localhost" />
    </authentication>
    <machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" validation="SHA1" decryption="Auto"/>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
</system.web>

我将Startup.Auth.cs更新为:

app.UseCookieAuthentication(new CookieAuthenticationOptions {
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    CookieDomain = ".localhost"
});

但仍然没有维护跨子域的登录。我还需要做些什么来使其发挥作用?

1 个答案:

答案 0 :(得分:0)

您需要跨域共享Cookie。从您的代码中,您似乎已将cookie的域设置为localhost的子域。问题可能出在IIS中针对子域的绑定中。检查这两个stackoverflow答案以获取帮助。

Share cookie between subdomain and domain

How to make subdomain on my localhost?

编辑:

正如您在评论中所说,Cookie不会根据请求发送到子域。您没有在代码中给出Cookie的任何到期日期。通过设置有效的时间跨度来给出到期日

    int days = 0, hours = 0, minutes = 0, seconds = 0;
    app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions
    {
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
        LoginPath = new PathString("/Account/Login"),
        CookieDomain = ".localhost",
        ExpireTimeSpan = new System.TimeSpan(days, hours, minutes, seconds)
    });