两个ASP.NET MVC项目实例并行工作时的授权

时间:2017-03-31 18:35:24

标签: asp.net-mvc

我有一个ASP.NET MVC项目。我有几个网站作为该项目的实例在同一台服务器上工作,例如在8000和8001端口上。当我在8000上授权时,如果我在8001上获得授权,我将自动在8001网站上注销,反之亦然。有什么问题?是关于饼干的吗?有什么方法可以解决它?

2 个答案:

答案 0 :(得分:1)

您可以通过放入web.config

为每个网站指定自定义Cookie名称
<authtentication model="Forms">
    <forms name="Cookie_8000" loginUrl="urlhere" />
</authentication>

ASP.NET MVC默认使用cookie来存储有关已签名用户的信息。当网站投入生产时,除非它们在同一个域上运行,否则它应该不是问题

然而,在开发工作区上运行时,您可以简单地指定cookie名称,就像之前我说的几行一样。甚至是在Startup.Auth.cs类中配置该cookie的更好解决方案。

以下是创建新项目时的默认配置:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        // Enables the application to validate the security stamp when the user logs in.
        // This is a security feature which is used when you change a password or add an external login to your account.  
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
}); 

您可以在此处指定Cookie的工作方式,并且应该是:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    LoginPath = new PathString("/Account/Login"),
    Provider = new CookieAuthenticationProvider
    {
        // Enables the application to validate the security stamp when the user logs in.
        // This is a security feature which is used when you change a password or add an external login to your account.  
        OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
            validateInterval: TimeSpan.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    },
    CookieName = "website1.cookie" // Specify the cookie name
}); 

答案 1 :(得分:0)

问题似乎是这些网站共享主域名。因此,他们还可以共享身份验证cookie。

例如,http://site1.example.comhttp://site2.example.com

您可以执行以下操作之一,不要在子域之间共享Cookie。

选项1

web.config

中为每个网站指定不同的计算机密钥
<system.web>
  <machineKey decryptionKey="..." validationKey="..." />
</system.web>

选项2

web.config 表单标记

中指定完整域名
<authentication mode="Forms">
   <forms loginUrl="~/Account/Login" timeout="2880" domain="site1.example.com" />
 </authentication>