【问题标题】:Identity user .net 4.6 and .net core 2.2身份用户 .net 4.6 和 .net core 2.2
【发布时间】:2020-04-17 11:38:11
【问题描述】:

我有以下情况: .net 4.6 Web 表单应用程序正在 f.e. 中运行。 domain.com 和用户使用身份 2.2 注册和登录。 有一些管理员用户具有Administrator 的角色。 还有一个子域 f.e. 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。

提前致谢!

【问题讨论】:

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


    【解决方案1】:

    如果您想尝试第二种方法,请尝试使用来自this github repository 的 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 core app中):

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

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

    【讨论】:

    • 您好@Daniel P,感谢您的回复。您能否分享一些来自 .net 4.6 和 core 2.2 的代码,看看您是如何使用这种方法的?提前致谢!
    • 假设您在 4.6 中使用 cookie 身份验证并生成 cookie,您需要在 .net 核心中创建自己的“ISecureDataFormat”以取消对 cookie 的保护,然后设置“TicketDataFormat”属性调用 AddCookie 扩展时。在答案中添加了示例。
    猜你喜欢
    • 2020-10-15
    • 2019-07-15
    • 1970-01-01
    • 2019-10-30
    • 2018-12-21
    • 2019-09-01
    • 2018-04-01
    • 2018-02-08
    • 1970-01-01
    相关资源
    最近更新 更多