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

时间:2017-12-06 12:35:11

标签: asp.net-mvc cookies webforms share

我们有两个ASP应用程序在同一台服务器上运行(在不同的子域中),第一个是Web Forms应用程序(我称之为App A),新的应用程序是APS.NET MVC应用程序(App B)。

App B需要登录App A restrited区域,我在网上看到有关在不同应用程序中共享cookie的信息,但是,在我的测试中,重定向有效,但无法找到cookie。 / p>

以下是我在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部分是我甚至无法调试应用程序来检查值:(

有人可以帮助我吗?

编辑1

我在this page中遵循了这些说明,有时我正确地获得了重定向&#34;#34;但是根本没有会话。

这里是代码(在应用程序中我在这里找到了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)。

我的想法是ApplicationName App A与App B不同,但我尝试更改this page并且它没有用。

有人能帮助我吗?

3 个答案:

答案 0 :(得分:2)

以下示例显示了Web.config文件的“身份验证”部分。除非另有说明,否则名称,保护,路径,validationKey,validation,decryptionKey和decryption属性在所有应用程序中必须相同。同样,用于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>

答案 1 :(得分:0)

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

单点登录概念:

我创建了三个项目。第一个项目(website1)提供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>

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

WEBSITE1:

的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应用程序机器密钥和解密密钥必须相同,否则将无法正常工作。

HomeController.cs

public class HomeController : Controller
{
    [Authorize]
    public ActionResult Index()
    {
        var name = User.Identity.Name;
        return View();
    }
}

我们启动website1时授权属性不允许查看主页。它将重定向到身份验证页面,一旦身份验证成功,在返回URL的帮助下页面将被重定向并返回到website1主页。 SSO应用程序在相同的机器和解密密钥配置的帮助下共享经过身份验证的用户信息。

您可以创建自定义主体并在您的应用程序中使用它。

答案 2 :(得分:0)

我会采用不同的方法来分享&#34; cookie&#34;不同网站上的价值观。

基于以下事实:

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

然后我会做以下事情:

  • 只要您在网站A上创建/更新/删除cookie,您就需要将此信息保存在本网站的数据库中。您需要将其存储在那里,同时直接在网站B DB上保存相同的值,或者为您调用正在执行此操作的服务。
  • 然后,在更新列表的操作上,取决于您,您只能在登录操作,或者您可以在每个请求中进行快速查询,或者您只需每5分钟左右使Cookie过期,以便刷新它们,你在这里有灵活性。