混淆电子邮件? [英] Obfuscate email?

查看:82
本文介绍了混淆电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们首先说我有一个非常大的项目,该项目的一部分是获取用户恢复操作状态和用户电子邮件,然后通过服务层将其发送回应用程序的前端.问题是,电子邮件需要在后端进行更改,因此它不会发送纯文本.我的意思是,当在后端填充该值时,我需要一些代码对其进行修改,以使其具有如下格式:j*****e@domain.com.这绝对需要在我正在使用的方法中完成(实际上这不是很大).这是我拥有的方法,它将从同一个类中的另一个方法中获取状态,以及获取用户的电子邮件:

Let's start by saying I have a very large project, part of this project is to grab a user recovery action status, and a user email, and send it through a service layer back to the front end of the application. The catch is, the email needs to be altered on the back end so it doesn't get sent plain text. What I mean by this is, when the value gets populated on the back end, I need to have some code to modify it so it will have a format like this: j*****e@domain.com. This absolutely needs to be done in the method that I'm working on(which honestly isn't very big). Here is the method I have that will grab the status from another method within the same class, as well as grabbing the email of the user:

public CredentialRecoveryResponse RecoveryResponse(CredentialRecoveryRequest request)
    {
        CredentialRecoveryResponse response = new CredentialRecoveryResponse();
        response.Status = RecoverCredentials(request);
        if (response.Status == UserRecoveryActionStatus.Success)
        {
            User usr = UserRepository.GetByID(request.UserID);
            response.Email = usr.EmailAddress;
        }

        return response;
    }

以某种方式,在此方法中,我需要获取usr.EmailAddress并对其进行修改,以将"@ domain.com"部分之前的第一个和最后一个字符除外的所有字符的值都阻止"或将其值更改为"*".在这种方法中,有没有一种快速简便的方法可以避免整个电子邮件地址都不会通过电传回去?

Somehow, inside this method, I need to take that usr.EmailAddress and modify it do "block" or change the values to "*" for all characters except the first and last characters before the "@domain.com" portion. Is there a quick and easy way to do this within the method that way the whole email address isn't getting sent back through the wire?

推荐答案

这是一招:

private static string ObfuscateEmail(string email)
{
     return Regex.Replace(email, "^(?<name>[^@]+)", m => {
          string match = m.Groups["name"].Value;
          return match[0] + new String('*', match.Length - 1);
     });
}

这是做什么的?

  1. 该方法使用Regex.Replace并传递一个lambda函数进行实际替换
  2. 正则表达式模式简单地说,将@符号左侧的所有内容都匹配起来,并创建一个名为"name"的命名组.
  3. 然后,lambda函数使用匹配的第一个字符,并使用String方法(char,int)的重载向其附加一系列星号,该方法将char重复N次.由于第一个字符未混淆,因此这里是N-1.

这篇关于混淆电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆