简体   繁体   English

跨子域的ASP.NET Identity Cookie

[英]ASP.NET Identity Cookie across subdomains

For forms authentication I used this in web.config (note the domain attribute): 对于表单身份验证,我在web.config中使用了它(请注意域属性):

<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? 如何在Mvc 5中为新的ASP.NET Identity Framework配置子域之间的单点登录?

More Info: 更多信息:

I am creating a multitenant application. 我正在创建一个多租户应用程序。 Each client will be on a subdomain: 每个客户端都在子域上:

client1.myapp.com client1.myapp.com

client2.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. 我希望用户能够登录到client1.myapp.com ,然后转到client2.myapp.com并仍然登录。使用表单身份验证很容易。 I'm trying to figure out how to do it with the new Identity Framework. 我正在试图弄清楚如何使用新的Identity Framework来实现它。

EDIT 编辑

Here is the code that eventually worked for me: 这是最终为我工作的代码:

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

In Startup.Auth.cs, you will see something like: 在Startup.Auth.cs中,您将看到如下内容:

for RC: 对于RC:

app.UseSignInCookies();

This was removed in RTM and replaced with the explicit configuration of the cookie auth: 这已在RTM中删除,并替换为cookie身份验证的显式配置:

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

The CookieAuthenticationOptions class has a CookieDomain property which is what you are looking for I believe. CookieAuthenticationOptions类有一个CookieDomain属性,我相信你正在寻找它。

This was driving me crazy until I learned that Identity 2.0 still depends on the machine key to encrypt the Authentication cookie. 这让我疯狂,直到我得知Identity 2.0仍然依赖于机器密钥来加密身份验证cookie。 So if you want two instances of the same application on different sub-domains then you need to set the same machine key for each application. 因此,如果您需要在不同子域上使用同一应用程序的两个实例,则需要为每个应用程序设置相同的机器密钥。

So in summary: 总结如下:

  1. CookieDomain = ".myapp.com" CookieDomain =“。myapp.com”
  2. Set identical machine keys in each application's web config 在每个应用程序的Web配置中设置相同的机器密钥

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

This answer led me to setting the values: Does ASP.NET Identity 2 use machinekey to hash the password? 这个答案让我设置了值: ASP.NET Identity 2是否使用machinekey来密码密码?

You need to set up in web.config the same machineKey for ALL websites/applications. 您需要在web.config中为所有网站/应用程序设置相同的machineKey。

All websites MUST HAVE at least this configuration. 所有网站必须至少具有此配置。

http://msdn.microsoft.com/en-us/library/w8h3skw9(v=vs.85).aspx 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>

This is an example 这是一个例子

In the Startup.Auth.cs file, add the CookieDomain parameter with your domain: 在Startup.Auth.cs文件中,在您的域中添加CookieDomain参数:

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

Then for all websites you need to set a unique machine key. 然后,对于所有网站,您需要设置唯一的机器密钥。 The easiest way to generate a new one is using IIS: 生成新的最简单方法是使用IIS:

Find the "Machine Key" option on your site: 在您的网站上找到“机器密钥”选项:

在此输入图像描述

Click the "Generate Keys" button to get your keys. 单击“生成密钥”按钮以获取密钥。

在此输入图像描述

Finally, the above process will add the following to your web.config and you need to ensure that this is copied into each of your sites. 最后,上面的过程会将以下内容添加到您的web.config ,您需要确保将其复制到您的每个站点中。

<machineKey
  validationKey="DAD9E2B0F9..."
  decryptionKey="ADD1C39C02..."
  validation="SHA1"
  decryption="AES"
/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM