43

For forms authentication I used this in web.config (note the domain attribute):

<authentication mode="Forms">
  <forms loginUrl="~/Account/Login" timeout="2880" name=".ASPXAUTH" protection="Validation" path="/" domain=".myserver.dev" />
</authentication>

How is a single sign-on across subdomains configured for the new ASP.NET Identity Framework in Mvc 5?

More Info:

I am creating a multitenant application. Each client will be on a subdomain:

client1.myapp.com

client2.myapp.com

I want a user to be able to sign on to client1.myapp.com and then go to client2.myapp.com and still be signed in. This was easy with forms authentication. I'm trying to figure out how to do it with the new Identity Framework.

EDIT

Here is the code that eventually worked for me:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
  AuthenticationType = "Application",
  LoginPath = "/Account/Login",
  CookieDomain = ".myapp.com"
});
4

4 回答 4

34

在 Startup.Auth.cs 中,您将看到如下内容:

对于钢筋混凝土:

app.UseSignInCookies();

这已在 RTM 中删除并替换为 cookie 身份验证的显式配置:

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

CookieAuthenticationOptions 类有一个 CookieDomain 属性,我相信这是您正在寻找的。

于 2013-10-07T16:49:47.723 回答
15

这让我发疯,直到我了解到 Identity 2.0 仍然依赖机器密钥来加密身份验证 cookie。因此,如果您希望同一应用程序在不同子域上的两个实例,那么您需要为每个应用程序设置相同的机器密钥。

总而言之:

  1. CookieDomain = ".myapp.com"
  2. 在每个应用程序的 Web 配置中设置相同的机器密钥

    <system.web>
      <machineKey decryptionKey="EEEB09D446CCFE71B82631D37DEDCC917B8CB01EC315" validationKey="60E4EFE8DD26C4BF8CDAEDCA10716C85820839A207C56C8140DB7E32BE04630AD631EDF25C748D0F539918283C5858AF456DBE208320CFFA69244B4E589" />
    </system.web>
    

这个答案让我设置了这些值: Does ASP.NET Identity 2 use machinekey to hash the password?

于 2014-07-12T01:32:11.363 回答
13

您需要在 web.config 中为所有网站/应用程序设置相同的 machineKey。

所有网站都必须至少具有此配置。

http://msdn.microsoft.com/en-us/library/w8h3skw9(v=vs.85).aspx

<system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login" timeout="2880" name=".ASPXAUTH" protection="Validation" path="/" domain=".myserver.dev" />
    </authentication>
    <machineKey validationKey="C50B3C89CB21F4F1422FF158A5B42D0E8DB8CB5CDA1742572A487D9401E3400267682B202B746511891C1BAF47F8D25C07F6C39A104696DB51F17C529AD3CABE" decryptionKey="8A9BE8FD67AF6979E7D20198CFEA50DD3D3799C77AF2B72F" validation="SHA1" decryption="Auto"/>
  </system.web>

这是一个例子

于 2013-10-11T15:16:45.533 回答
13

在 Startup.Auth.cs 文件中,CookieDomain使用您的域添加参数:

var cookieAuthenticationOptions = new CookieAuthenticationOptions
{
    AuthenticationType  = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath           = new PathString("/Account/Login"),
    CookieDomain        = ".mydomain.com"
};

然后对于所有网站,您需要设置一个唯一的机器密钥。生成新文件的最简单方法是使用 IIS:

在您的网站上找到“机器密钥”选项:

在此处输入图像描述

单击“生成密钥”按钮以获取您的密钥。

在此处输入图像描述

最后,上述过程会将以下内容添加到您的web.config网站中,您需要确保将其复制到您的每个网站中。

<machineKey
  validationKey="DAD9E2B0F9..."
  decryptionKey="ADD1C39C02..."
  validation="SHA1"
  decryption="AES"
/>
于 2015-09-28T10:18:23.970 回答