我们有两个ASP应用程序在同一台服务器上运行(在不同的子域),第一个是一个Web表单应用程序(我称之为App A),而新的一个是APS.NET MVC应用程序(App B)。
App B需要在App A的重设区域登录,我在网上看到了关于在不同应用程序中共享cookie的内容,但是,在我的测试中,重定向成功了,但是cookie却找不到了。
下面是我在App 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的密钥是认证的关键所在 Web.Config
<authentication mode="Forms">
<forms loginUrl="/Login/Acess" enableCrossAppRedirects="true" path="/" name=".ASPXAUTHX" domain="dev.com.br" protection="All" />
</authentication>
Wrost的部分是我甚至不能调试应用程序来检查值 :(
谁能帮帮我?
EDIT 1
我按照这些指示在 本页 而且,有时我已经得到了重定向 "正确",但没有会话在所有。
下面是代码(在App中,我在这里接收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为空)。
我的想法是ApplicationName App A和App B不一样,但我试着在以下地方修改 本页 但它没有工作。
谁能帮帮我?
下面的例子显示了Web.config文件的认证部分。除非另有说明,否则所有应用程序的名称、保护、路径、validationKey、validation、decryptionKey和解密属性必须是相同的。同样,加密和验证密钥以及用于 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>
借助于 Single Sign On
或 WCF STS
概念,您可以将cookie分享给任何数量的应用程序。
我创建了三个项目。第一个项目(website1)提供了用户界面和所有需要的功能。第二个项目(website2)提供服务相关的功能。第三个项目(SSO)处理认证相关功能和用户管理相关的东西。
SSO项目提供登录用户的相关信息(Cookies)。
Web.config .AccountController.cs
<machineKey validationKey="(Machine Key)" decryptionKey="(Decryption Key)" validation="HMACSHA256" decryption="AES"/>
<authentication mode="Forms">
<forms name="SingleSignOn" loginUrl="http://(sso hosted application)/Account/login" timeout="480" slidingExpiration="true"/>
</authentication>
帐户控制器.cs
public class AccountController : Controller
{
[AllowAnonymous]
[HttpGet]
public ActionResult Login(string returnUrl)
{
if (Request.IsAuthenticated)
{
return RedirectToAction("Index", "Home");
}
ViewBag.ReturnUrl = returnUrl;
return View();
}
[AllowAnonymous]
[ValidateAntiForgeryToken]
[HttpPost]
public ActionResult Login(string username, string password, string returnUrl)
{
FormsAuthentication.SetAuthCookie(username, false);
if (!string.IsNullOrEmpty(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToAction("Index", "Home");
}
}
Web.config
<machineKey validationKey="(Machine Key)" decryptionKey="(Decryption Key)" validation="HMACSHA256" decryption="AES"/>
<authentication mode="Forms">
<forms name="SingleSignOn" loginUrl="http://(sso hosted application)/Account/login" timeout="480" slidingExpiration="true"/>
</authentication>
注意:你的sso应用机器密钥和解密密钥必须一致,否则将无法使用。
首页控制器.cs
public class HomeController : Controller
{
[Authorize]
public ActionResult Index()
{
var name = User.Identity.Name;
return View();
}
}
当我们启动website1时,授权属性不允许查看主页。当我们启动website1时,授权属性不允许查看主页,它会重定向到认证页面,一旦认证成功,在return url的帮助下,页面会被重定向并返回到website1的主页。单点登录应用在同一台机器和解密密钥配置的帮助下共享已认证的用户信息。
你可以创建自定义委托人并在你的应用程序中使用它。
我建议采用不同的方法在不同网站上共享 "cookie "值。
基于以下事实。
那么我会做以下工作。