使用c#在asp.net中忘记密码时发送邮件给电子邮件? [英] send message to email when forget password in asp.net using c# ?

查看:117
本文介绍了使用c#在asp.net中忘记密码时发送邮件给电子邮件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好您好

请我需要简单的代码将其添加到我的asp.net网络应用程序项目

点击(忘记密码按钮)

使用密码发送电子邮件给用户

后面使用c#代码:)

请帮助我:)

解决方案

< blockquote>它实际上是一个简单的代码,只有一个巨大的缺点 - 安全性!

您已经做了严重的安全漏洞 - 将密码作为纯文本存储在您的数据库中。从来没有这样做,但存储单向哈希...

现在你可以理解,没有办法发送密码,因为你没有...(但即使你有它是一个通过电汇发送密码的其他严重安全漏洞)...

在您的网站中执行一个页面,使用户能够设置新密码并且根本不向他发送任何信息......


有许多方法可以执行此方法。但是,让我向您纠正,您将不会处理点击事件(除非您使用的是Web窗体框架),但您将导航到应用程序中的新页面,您将从该页面发送电子邮件。要做到这一点,请完成以下几个阶段,





  1. 忘记密码意味着他不知道他的密码,你已经保存了哈希密码;您应该始终考虑在保存密码之前对密码进行哈希处理,这是保护密码的唯一安全方法。
  2. 因此,您必须向他发送另一个令牌,他可以使用密码并保存密码在您的数据库中用于登录。
  3. 使用电子邮件客户端或代码隐藏来使用SMTP将电子邮件发送到指定的电子邮件地址;电子邮件地址将来自用户的电子邮件地址。
  4. 将该令牌附加到正文中,然后发送给他。他将访问带有令牌的链接,您可以验证剩余的内容。





ASP.NET完全在.NET框架上工作并且可以使用Visual C#代码来执行所有活动,因此如果您可以查看可以为您工作的C#相关解决方案,则无需深入了解特定于ASP.NET的解决方案。



无论如何,在ASP.NET中,你可以使用Crypto类来散列密码(阅读我的文章 [ ^ ]此处你也可以使用ASP.NET框架的WebMail类将电子邮件发送给用户。阅读我的文章 [ ^ ]了解如何发送电子邮件至用户。


 使用 System.Net; 
使用 System.Net.Mail;

private static string SMTP_SERVER = System.Configuration.ConfigurationManager.AppSettings [ SMTP_SERVER];
私有 静态 字符串 FROM_ADDRESS = System.Configuration.ConfigurationManager.AppSettings [ FROM_ADDRESS];

public static void SendEmail( string toEmailAddress, string subject, string 消息)
{
尝试
{
SmtpClient client = new SmtpClient(SMTP_SERVER);
MailMessage mailMessage = new MailMessage();
mailMessage.From = new MailAddress(FROM_ADDRESS);
mailMessage.To.Add(toEmailAddress);

mailMessage.Subject = subject;
message = message
mailMessage.Body = message;
mailMessage.IsBodyHtml = true ;
client.Send(mailMessage);
}
catch (例外情况)
{
// 待办事项:记录异常;
}
}


Hello all
please i need simple code to add it to my asp.net web application project
on click ( Forget password button )
send email to user with password
using c# code behind :)
please help me :)

解决方案

It is really a simple code, with only one huge drawback - security!
You already did a serious security breach - stored the password as plain text in you database. Never done it, but store a one-way hash...
Now you can understand that there is no way to send the password as you have no...(but even you have it is an other serious security breach to send the password over the wires)...
Do a page in your site that enables the user to set a new password and send nothing to him at all...


There are a bunch of methods to perform this method. However, let me correct you that you will not be handling the click event (unless you're using the Web Forms framework) but you will be navigating to a new page in your application where you will be sending the email from. To accomplish this, go through the following stages,


  1. Forgot your password means that he doesn't know his password at all, and you've already saved the hashed password; you should always consider hashing the passwords before saving them it is the only secure way to secure the password.
  2. So, you will have to send him another token, that he can use to use a password and save it in your database for login purposes.
  3. Use an Email client, or a code-behind to use the SMTP to send the email to a specified email address; email address would be coming from the user's email address.
  4. Attach that token in the body, send it to him. He will visit the link with the token and you can validate the remaining stuff.



ASP.NET is entirely working on .NET framework and can use Visual C# code, to perform all activities, so there is no need to look deeper into an ASP.NET specific solution, if you can see for a C# related solution that can also work for you.

Anyways, in ASP.NET, you can hash the passwords using the Crypto class (read my article[^] on this) you can also use the WebMail class of ASP.NET framework to send the emails to the users. Read my article[^] to learn on sending the emails to the users.


using System.Net;
using System.Net.Mail;

private static string SMTP_SERVER = System.Configuration.ConfigurationManager.AppSettings["SMTP_SERVER"];
private static string FROM_ADDRESS = System.Configuration.ConfigurationManager.AppSettings["FROM_ADDRESS"];

 public static void SendEmail(string toEmailAddress, string subject, string message)
        {
            try
            {
                SmtpClient client = new SmtpClient(SMTP_SERVER);
                MailMessage mailMessage = new MailMessage();
                mailMessage.From = new MailAddress(FROM_ADDRESS);
                mailMessage.To.Add(toEmailAddress);
                
                mailMessage.Subject = subject;
                message = message
               mailMessage.Body = message;
                mailMessage.IsBodyHtml = true;
                client.Send(mailMessage);
            }
            catch (Exception ex)
            {
                //TO DO: Log exception;
            }
        }


这篇关于使用c#在asp.net中忘记密码时发送邮件给电子邮件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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