首页 > 解决方案 > 身份用户 .net 4.6 和 .net core 2.2

问题描述

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

我做了什么:

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

提前致谢!

标签: asp.net-identityasp.net-core-2.2asp.net-4.6

解决方案


如果您想尝试第二种方法,请尝试使用此 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
                }
            };
        });

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


推荐阅读