0

我有以下场景: .net 4.6 Web 表单应用程序在 fe 中运行,domain.com用户使用身份 2.2 注册和登录。有一些管理员用户具有Administrator. 还有一个子域 fe admin.domain.com,在 .net core 2.2 中制作,现在我希望管理员用户只能访问子域。

我做了什么:

  • 第一次尝试是生成一个链接,admin.domain.com?email=<hashed email>&datetime=<hashed datetime>并将此链接显示给域中的管理员用户,以便跳转到子域。然后在子域中,我尝试读取查询字符串并确定用户是否有权访问子域。在这种方法中,我遇到了很多问题,我认为这不是正确的解决方案。
  • 我的第二种方法也是对子域使用 Identity 用户,但我意识到 2 个身份(.NET Framework 4.6 和 Core 2.2)是不同的,我没有设法让它工作,例如我需要已经域中的登录用户自动在子域中获得授权。此外,子域没有任何注册程序,仅存在于 domain.com

我想知道是否有一个强大的解决方案可以解决我的 2 个身份问题,目前我需要在 domain.com 中至少保留身份 2.2。

提前致谢!

4

1 回答 1

0

如果您想尝试第二种方法,请尝试使用此 github 存储库中的 asp.net 票证桥。我用它来促进在 asp.net 核心和 Web 表单身份验证之间共享单一身份 - 只需记住同步加密密钥......希望这会有所帮助!

您将需要创建自己的“ISecureDataFormat”实现:

public class OWINAuthenticationDataFormat<TData> : ISecureDataFormat<TData>
    where TData : AuthenticationTicket
{
    public OWINAuthenticationOptions Options { get; set; }

   ...
    public string Protect(TData data)
    {
        return Protect(data, null);
    }
    ..
    public string Protect(TData data, string purpose)
    {
        string decryptionKey = Options.DecryptionKey;
        string validation = Options.ValidationMethod;
        string validationKey = Options.ValidationKey;
        string decryption = Options.EncryptionMethod;

        var claimsIdentity = data.Principal.Identity as ClaimsIdentity;
        var authTicket = new OwinAuthenticationTicket(claimsIdentity, data.Properties);

        // Encrypt the token
         return MachineKeyTicketProtector.ProtectCookie(authTicket, decryptionKey, validationKey, decryption, validation);
    }
    ...
    public TData Unprotect(string protectedText)
    {
        return Unprotect(protectedText, null);

    }
  ...
    public TData Unprotect(string protectedText, string purpose)
    {
        string decryptionKey = Options.DecryptionKey;
        string validation = Options.ValidationMethod;
        string validationKey = Options.ValidationKey;
        string decryption = Options.EncryptionMethod;
        // Decrypt the token
        var ticket = MachineKeyTicketUnprotector.UnprotectCookie(protectedText, decryptionKey, validationKey, decryption, validation);

        return new AuthenticationTicket(new System.Security.Claims.ClaimsPrincipal(ticket.Identity), ticket.Properties, "") as TData;
    }
}

之后,在添加 cookie 身份验证时使用它(仍在 asp.net 核心应用程序中):

.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, opts =>
        {
            opts.Cookie = new CookieBuilder()
            {
                Domain = CookieDomain,
                Name = CookieName,
                Path = CookiePath,
                SecurePolicy = CookieSecurePolicy.Always
            };


            opts.TicketDataFormat = new OWINAuthenticationDataFormat<AuthenticationTicket>()
            {
                Options = new OWINAuthenticationOptions()
                {
                    DecryptionKey = DecryptionKey,
                    EncryptionMethod = DecryptionAlgorithm,
                    ValidationKey = ValidationKey,
                    ValidationMethod = ValidationAlgorithm
                }
            };
        });

请记住在两个应用程序中使用相同的签名密钥和算法!

于 2020-01-13T07:35:55.583 回答