如何在不同的ASP.NET应用程序中共享cookies?

问题描述 投票:2回答:3

我们有两个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不一样,但我试着在以下地方修改 本页 但它没有工作。

谁能帮帮我?

asp.net-mvc cookies webforms share
3个回答
2
投票

下面的例子显示了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>

0
投票

借助于 Single Sign OnWCF STS 概念,您可以将cookie分享给任何数量的应用程序。

单点登录的概念。

我创建了三个项目。第一个项目(website1)提供了用户界面和所有需要的功能。第二个项目(website2)提供服务相关的功能。第三个项目(SSO)处理认证相关功能和用户管理相关的东西。

SSO项目提供登录用户的相关信息(Cookies)。

SSO项目配置。

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");
    }
}

网站1.Web.config : AccountController.cs

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的主页。单点登录应用在同一台机器和解密密钥配置的帮助下共享已认证的用户信息。

你可以创建自定义委托人并在你的应用程序中使用它。


0
投票

我建议采用不同的方法在不同网站上共享 "cookie "值。

基于以下事实。

  • 你可以访问两个网站的源代码
  • 两个网站都可以访问另一个网站的至少一个表,或者至少可以从另一个网站调用一些服务。
  • 所有的Cookie都是在C#代码上创建的,而不是在javascript上创建的(但这一点并不是很重要,因为你仍然可以解决这个问题)。
  • 有一种方法可以匹配网站A和B的1个用户。

那么我会做以下工作。

  • 只要你在网站A上创建了updatedelete一个cookie,那么你就需要把这个信息保存在这个网站的数据库里。你需要把它存储在那里,同时你直接把同样的值保存在B网站的DB上,或者调用为你做这些事情的服务。
  • 然后,在更新列表的动作上,是由你决定的,你只在登录的动作上,或者以某种方式,你可以在每个请求中进行快速查询,或者你干脆每隔5分钟左右过期一次cookie,以刷新它们,你在这里有灵活性。
© www.soinside.com 2019 - 2024. All rights reserved.