如何在实体框架中通过电子邮件发送密码5 [英] How to send password via email in entity framework 5

查看:95
本文介绍了如何在实体框架中通过电子邮件发送密码5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过电子邮件发送密码..但是当输入电子邮件地址并查看..显示错误

如何解决此错误,

i am send password via email..butt when enter the email address and sed the..show the error
how to resolve this error,,

Server Error in '/' Application.

The ConnectionString property has not been initialized.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.InvalidOperationException: The ConnectionString property has not been initialized.

Source Error: 


Line 118:                SqlDataAdapter dAdapter = new SqlDataAdapter(command);
Line 119:                command.Parameters.AddWithValue("@Email", Email);
Line 120:                connection.Open();
Line 121:                dAdapter.Fill(dsPwd);
Line 122:





我有什么试过:





What I have tried:

<pre lang="c#">public ActionResult ForgotPassword(User s)
        {
            using (userEntities2 db = new userEntities2())
            {

                string Email = Request.Form["Email"];
                string Msg = Request.Form["Message"];


                string strConnection = ConfigurationSettings.AppSettings["userEntities2"];
                string strSelect = "SELECT UserName,Password FROM user WHERE Email = @Email";

                SqlConnection connection = new SqlConnection(strConnection);
                SqlCommand command = new SqlCommand();
                command.Connection = connection;
                command.CommandType = CommandType.Text;
                command.CommandText = strSelect;

                DataSet dsPwd = new DataSet();
                SqlDataAdapter dAdapter = new SqlDataAdapter(command);
                command.Parameters.AddWithValue("@Email", Email);
                connection.Open();
                dAdapter.Fill(dsPwd);

                connection.Close();

                if (dsPwd.Tables[0].Rows.Count > 0)
                {
                    string MailingAddress = ConfigurationManager.AppSettings["MailingAddress"].ToString();
                    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(MailingAddress, Email);
                    SmtpClient client = new SmtpClient();
                    message.Subject = "Forget Password";
                    message.IsBodyHtml = true;
                    message.Body = "Username: " + dsPwd.Tables[0].Rows[0]["UserName"] + "<br><br>Password: " + dsPwd.Tables[0].Rows[0]["Password"] + "<br><br>";

                    client.Host = "smtp.mail.yahoo.com";
                    client.Port = 587;
                    client.Credentials = new System.Net.NetworkCredential
                    ("email", "password");
                    client.EnableSsl = true;
                    client.Send(message);
                    Response.Write("&lt;script LANGUAGE='JavaScript'>alert('Email has Sent')&lt;/script>");

                }
                else
                {
                    Response.Write("&lt;script LANGUAGE='JavaScript'>alert('Email Not Register')&lt;/script>");
                }


                return View(s);
            }
        }</pre>

推荐答案

您的连接字符串适用于Entity Framework。 SqlConnection需要一个不同的。但是,由于您的项目是实体框架项目,我认为您没有理由想在此处绕过实体框架。实际上,您使用 -Directive在方法顶部初始化了一个带有的EF-DBContext,然后根本不使用它。



为了坚持你的问题,我建议你删除所有的Sql **** - 东西,并用EF查询替换它。 但是: 请看一下这些文章:



简单安全密码验证说明 [ ^ ]



Troy Hunt:您想要了解的有关构建安全密码重置功能的所有内容 [ ^ ]
Your connection string is meant for Entity Framework. SqlConnection would require a different one. But as your project is an Entity Framework-project I see no reason why you would want to bypass Entity Framework here. You actually do initialize an EF-DBContext with that using-Directive at the top of your method - which you then don't use at all.

To stick with your question I would suggest you remove all that Sql****-stuff and replace it with an EF-query. BUT: You should neither store passwords as plain text in your database nor send them via email. Please take a look at these articles:

Secure Password Authentication Explained Simply[^]

Troy Hunt: Everything you ever wanted to know about building a secure password reset feature[^]


查看你的问题历史记录你我真的多次问同样的问题。您询问有关将aspx代码转换为ado.net的问题,现在您尝试将Eintity Framework放在其上,并且您实际上并未在所显示的代码中使用任何EF功能。



您似乎提出问题,然后发布非常简短的建议回复,然后最后请求别人为您编写代码。你甚至做了一个非常简单的问题我几个星期前回答你在存储过程中缺少一个列。



这些代码中的任何一个甚至是你的吗?我建议你慢下来并学习基础知识,然后再深入了解。
Looking at you question history you have really asked this same question multiple times. You asked about converting aspx code to ado.net and now you trying to throw Eintity Framework on top of it and you are not actually using any EF feature in the code you have displayed.

You seem to ask questions and then post very short replies to advice and then finally ask someone to write the code for you. You even did that to a very simple question I answered a few weeks ago where you were missing a column in a stored procedure.

Is any of this code even your? I suggest you slow down and learn the basics before you get much further.


你必须为命令分配连接;



You have to assign the connection to the command;

command.Connection = connection;
SqlDataAdapter dAdapter = new SqlDataAdapter(command);





你没有使用实体框架,如上所述,你所做的事情有很多原因。



You're not using Entity Framework though, and as stated above what you're doing is bad for many reasons.


这篇关于如何在实体框架中通过电子邮件发送密码5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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