【问题标题】:SSO FormsAuthentication - 2 Applications - 1 x WebForms and 1 x MVCSSO FormsAuthentication - 2 个应用程序 - 1 个 WebForms 和 1 个 MVC
【发布时间】:2017-06-27 13:47:36
【问题描述】:

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

我有一个使用 FormsAuthentication 的现有 WebForms (W1) 应用程序。作为将其迁移到 MVC 的一部分,我们希望创建一个“旁边”的 MVC (M1) 应用程序并在那里实现新功能。 (目前将整个 W1 应用程序移植到 MVC 超出了范围。)现有的 FormsAuthentication 将被维护并用于两个站点。

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

  • w1.mydomain.com
  • m1.mydomain.com

期望

  1. 用户将登录到 W1 并在经过身份验证后访问 M1 url 以及 W1 url
  2. M1 应用程序需要“知道”哪个用户登录了
  3. 通过 W1 或 M1 注销都会将用户注销以使用其他应用程序

已实施的解决方案

登录:

  • 在域级别共享身份验证 cookie,即 mydomain.com
  • 在 W1 中成功登录后重定向到 M1 以设置用户身份验证 cookie
  • 从 M1 重定向回 W1

退出:

  • W1/M1 调用 FormsAuthentication.SignOut();然后重定向到另一边做同样的事情

配置

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 - 执行成功登录并重定向到 m1.mydomain.com/sso/login

B. m1.mydomain.com/sso/login - 为用户 test@mydomain.com 设置 cookie 并重定向到 w1.mydomain.com/BounceLoginReturn

问题

当我返回 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 控件登录的?

【问题讨论】:

  • 使用有效的“decryptionKey”和“validationKey”而不是“AutoGenerate”,它在所有应用程序中应该是相同的。验证和解密算法也必须相同。

标签: asp.net asp.net-mvc webforms forms-authentication


【解决方案1】:

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

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

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

然后使用右侧的“生成密钥”。

【讨论】:

    猜你喜欢
    • 2011-02-23
    • 2016-01-10
    • 2016-01-19
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多