我们有两个 ASP 应用程序在同一台服务器上运行(在不同的子域中),第一个是 Web 窗体应用程序(我将其称为应用程序 A),新的一个是 APS.NET MVC 应用程序(应用程序 B) .
应用程序B需要登录应用程序A的限制区域,我在网上看到有关在不同应用程序中共享cookie的信息,但是,在我的测试中,重定向有效,但找不到cookie。
以下是我在应用程序 B 中发送 cookie 的方法:
var log = auth.GetLogin(user, password, Request.ServerVariables["REMOTE_ADDR"], 1);
if (!log.isPasswordValid)
throw new Exception("user or password incorrect!");
FormsAuthentication.SetAuthCookie(user, false);
并且,在应用程序 A 中,我尝试获取 cookie 的方法如下:
if(HttpContext.Current.Request.Cookies["ASPXAUTH"] != null)
{
var user = httpContext.Current.Request.Cookies["ASPXAUTH"].Value;
Session["LoginUser"] = user;
}
ASPXAUTH key 是Web.Config
中的身份验证
<authentication mode="Forms">
<forms loginUrl="/Login/Acess" enableCrossAppRedirects="true" path="/" name=".ASPXAUTHX" domain="dev.com.br" protection="All" />
</authentication>
最糟糕的是我什至无法调试应用程序来检查值:(
有人可以帮助我吗?
编辑 1
我按照 this page 中的说明进行操作并且,有时我“正确”地获得了重定向,但根本没有 session 。
这是代码(在应用程序中,我收到了 cookie):
if (HttpContext.Current.Request.IsAuthenticated)
{
for (int i = 0; i < HttpContext.Current.User.Identity.Name.Length; i++)
{
userId += userId = HttpContext.Current.User.Identity.Name[i].ToString();
}
}
有时 userId 没有出现,它会在 Length 部分抛出异常(HttpContext.Current.User.Identity.Name 为 null)。
我的想法是应用程序A的ApplicationName与应用程序B不一样,但我尝试在this page中进行更改但它不起作用。
有人可以帮助我吗?
最佳答案
以下示例显示 Web.config 文件的身份验证部分。除非另有说明,否则所有应用程序中的名称、保护、路径、validationKey、validation、decryptionKey 和解密属性必须相同。同样,加密和验证 key 以及用于 cookie 数据的加密方案和验证方案必须完全相同。如果设置不匹配,则无法共享 cookie。
<configuration>
<system.web>
<authentication mode="Forms" >
<!-- The name, protection, and path attributes must match
exactly in each Web.config file. -->
<forms loginUrl="login.aspx"
name=".ASPXFORMSAUTH"
protection="All"
path="/"
timeout="30"
domain="MyWeb.com"
/>
</authentication>
<!-- Validation and decryption keys must exactly match and cannot
be set to "AutoGenerate". The validation and decryption
algorithms must also be the same. -->
<machineKey
validationKey="[your key here]"
decryptionKey="[your key here]"
validation="SHA1" />
</system.web>
</configuration>
关于asp.net-mvc - 如何在不同的 ASP.NET 应用程序中共享 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47674442/