Asp.net mvc 如何在不同的ASP.NET应用程序中共享cookie
我们有两个ASP应用程序运行在同一台服务器上(在不同的子域中),第一个是Web表单应用程序(我称之为应用程序a),新的一个是APS.NET MVC应用程序(应用程序B) 应用程序B需要登录到应用程序A限制区,我在网上看到了关于在不同应用程序中共享cookie的内容,但是,在我的测试中,重定向工作正常,但是找不到cookie 以下是我在应用程序B中发送cookie的方式:Asp.net mvc 如何在不同的ASP.NET应用程序中共享cookie,asp.net-mvc,cookies,webforms,share,Asp.net Mvc,Cookies,Webforms,Share,我们有两个ASP应用程序运行在同一台服务器上(在不同的子域中),第一个是Web表单应用程序(我称之为应用程序a),新的一个是APS.NET MVC应用程序(应用程序B) 应用程序B需要登录到应用程序A限制区,我在网上看到了关于在不同应用程序中共享cookie的内容,但是,在我的测试中,重定向工作正常,但是找不到cookie 以下是我在应用程序B中发送cookie的方式: var log = auth.GetLogin(user, password, Request.ServerVariables
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>
有时,我得到了“正确”的重定向,但根本没有会话
以下是代码(在应用程序a中,我收到了cookies):
if(HttpContext.Current.Request.IsAuthenticated)
{
for(int i=0;i
有时userId没有出现,它在长度部分抛出异常(HttpContext.Current.User.Identity.Name为null)
我的想法是ApplicationName应用程序A和应用程序B不一样,但我尝试在中进行更改,但没有成功
有人能帮我吗?下面的示例显示了Web.config文件的身份验证部分。除非另有说明,名称、保护、路径、validationKey、验证、解密密钥和解密属性在所有应用程序中都必须相同。同样,用于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>
借助于单一登录
或WCF STS
概念,您可以将cookie共享给任意数量的应用程序
单点登录概念:
我创建了三个项目。第一个项目(网站1)提供ui演示和所有必需的功能。第二个项目(website2)提供与服务相关的功能。第三个项目(SSO)处理与身份验证相关的功能和与用户管理相关的内容
SSO项目提供登录的用户相关信息(Cookies)
SSO项目配置:
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>
网站1:
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>
当我们启动网站1时,授权属性不允许查看主页。它将重定向到身份验证页面,一旦身份验证在返回url的帮助下成功,该页面将被重定向并返回到website1主页。SSO应用程序在同一台机器和解密密钥配置的帮助下共享经过身份验证的用户信息
您可以创建自定义主体并在应用程序中使用它 我会建议在不同的网站上共享“cookie”值的不同方法
基于以下事实:
- 您可以访问这两个网站的源代码
- 两个网站都可以访问另一个网站的至少一个表,或者至少可以从另一个网站调用某些服务
- 所有的cookie都是在C#代码上创建的,而不是在javascript上创建的(但这一点并不是那么严格,因为您仍然可以解决它)
- 有一种方法可以匹配网站a和B的1个用户
然后我会做以下几件事:
- 一旦您在网站a上创建/更新/删除cookie,则需要将此信息保存在此网站的数据库中。您需要将其存储在那里,同时您可以将相同的值直接保存在网站B DB上,或者致电为您执行此操作的服务
- 然后,更新列表的操作取决于您,您只需要登录操作,或者您可以在每个请求中进行快速查询,或者您只需每隔5分钟左右使cookie过期,为了刷新它们,您在这里有灵活性
如果您有两个不同的应用程序,您可以使用一些持久化层(如数据库)来持久化此信息,并在两个应用程序中检索它。如果它是一个较大的信息,您可以将其保存在xml中并传递到您的数据库,然后在下一个应用程序中再次使用它。我希望我可以这样做,但是,我不能在应用程序中触摸一个代码,它是一个热门的数据库:(让我问您,loginUrl=“login.aspx”
是destiny URL还是origin URL?它是在应用程序中提供登录验证的页面。只需确保项目中的“名称、保护、路径、验证密钥、验证、解密密钥”相同即可。
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");
}
}
<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>
public class HomeController : Controller
{
[Authorize]
public ActionResult Index()
{
var name = User.Identity.Name;
return View();
}
}