SSO FormsAuthentication - 2 个应用程序 - 1 x WebForms 和 1 x MVC

SSO FormsAuthentication - 2 Applications - 1 x WebForms and 1 x MVC

我已经阅读了很多帖子,虽然我已经收集了我需要的所有信息,但我无法让它工作。所以我希望有人能指出我正确的方向。或者,如果您有一个有效的虚拟项目,那就更好了。我看到的所有例子都没有专门针对我的要求2.

我有一个使用 FormsAuthentication 的现有 WebForms (W1) 应用程序。作为将其迁移到 MVC 的一部分,我们希望创建一个 MVC (M1) 应用程序 "alongside" 它并在那里实现新功能。 (目前无法将整个 W1 应用程序移植到 MVC。)现有的 FormsAuthentication 将被维护并用于两个站点。

两个应用程序都将 运行 在同一个 IIS 上,但在不同的子域下:

预期

  1. 用户将登录到 W1 并在通过身份验证后访问 M1 网址以及 W1 网址
  2. M1应用需要"know"哪个用户登录
  3. 通过 W1 或 M1 注销将使用户注销其他应用程序

已实施解决方案

登录:

注销:

配置

W1

web.config

<machineKey decryption="AES" validation="HMACSHA256" decryptionKey="AutoGenerate" validationKey="AutoGenerate" />
<authentication mode="Forms">
  <forms loginUrl="~/account/login" timeout="120" defaultUrl="~/" domain=".mydomain.com" />
</authentication>
<compilation targetFramework="4.6.1"></compilation>
<!-- requestValidationMode needs to remain -->
<httpRuntime targetFramework="4.6.1" requestValidationMode="2.0" 
             maxRequestLength="20480" executionTimeout="300" />

/account/login

上的登录控制
<asp:Login ID="idLogin" runat="server" ViewStateMode="Disabled"  DestinationPageUrl="~/sso/BounceLogin.aspx" >

BounceLogin.aspx 的隐藏代码:

public partial class BounceLogin : Page
{
    protected void Page_Load(object aSender, EventArgs aArgs)
    {
        Response.Redirect("https://m1.mydomain.com/sso/login");
    }
}

M1

web.config

<machineKey decryption="AES" validation="HMACSHA256" decryptionKey="AutoGenerate" validationKey="AutoGenerate" />
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<authentication mode="Forms">
  <forms loginUrl="https://w1.mydomain.com/account/login" timeout="120" domain=".mydomain.com" />
</authentication>

SSO 控制器:

public class SsoController : Controller
{
    [Authorize]
    // GET: Secure
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Status()
    {
            return View(new ViewModel
            {
                TheUser = User
            });
    }

    public ActionResult Logout()
    {
        FormsAuthentication.SignOut();
        return Redirect("https://w1.mydomain.com/");
    }

    public ActionResult Login()
    {
        FormsAuthentication.SetAuthCookie("test@mydomain.com", false);
        return Redirect("https://w1.mydomain.com/sso/BounceLoginReturn");
    }
}

互动次数

下面是我用测试用户登录时发生的情况:test@mydomain.com。 (为了简化代码,我省略了传递给 M1 的用户名)。

A. w1.mydomain.com/account/login - Perform Successful Login and redirect to m1.mydomain.com/sso/login

B. m1.mydomain.com/sso/login - Sets cookie for user test@mydomain.com and redirects to w1.mydomain.com/BounceLoginReturn

问题

当我return到w1.mydomain。com/BounceLoginReturn W1 仍然认为我没有登录并重定向到w1.mydomain.com/account/login。 (如果我在另一个浏览器选项卡中打开 M1,它会告诉我我以 user@test.com 身份登录)

我已经检查过 w1.mydomain.com 和 m1.mydomain.com 都为域 .mydomain.com 设置了相同的 cookie 值。

所以我在这里做错了什么让 W1 认为我没有登录,请记住我最初是通过它包含的 asp:Login 控件登录的?

如果其他人遇到此问题,那么解决方案很简单。我上面的代码在功能上是正确的。但是我确实需要使用硬编码密钥:

<machineKey decryption="AES" validation="HMACSHA256" decryptionKey="{Hard Coded Key Here}" validationKey="{Hard Coded Key Here}" />

有很多网站可以生成这些,但最简单的方法是使用 IIS 本身:

然后在右侧使用"Generate Keys"。