如何解决密码恢复中发生的错误?

时间:2016-11-05 17:44:30

标签: c# asp.net entity-framework

我正在使用带有EF的asp.net c#,并且当用户忘记密码时我试图重置密码。在那里,我发送电子邮件链接到用户提供的电子邮件,如果它在数据库中。

但是它会在 member = Membership.GetUser(foundemail); 上说

时给我一个错误
  

{"尝试初始化System.Data.SqlClient.SqlConnection对象时发生错误。为连接字符串提供的值可能有误,或者可能包含无效的语法。\ r \ nParameter name:connectionString"}

这是控制器中的代码

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult ResetPassword(ResetPasswordModel resetpasswordmodel)
    {
        if (ModelState.IsValid)
        {
            //User user;
            MembershipUser member;
            using (TheFoodyContext db = new TheFoodyContext())
            {
                /*var foundemail = (from e in db.Users
                                     where e.email == resetpasswordmodel.Email
                                     select e.email).FirstOrDefault();*/

                var foundemail = (db.Users.Find(resetpasswordmodel.Email)).email.ToString();

                if (foundemail != null)
                {

                    member = Membership.GetUser(foundemail);    
                }
                else
                {
                    member = null;
                }
            }

            if (member != null)
            {
                //Generate password token that will be used in the email link to authenticate user
                var token = WebSecurity.GeneratePasswordResetToken(member.Email);
                // Generate the html link sent via email
                string resetLink = "<a href='"
                   + Url.Action("ResetPasswordView", "Account", new { rt = token }, "http")
                   + "'>Reset Password Link</a>";
                // Email stuff
                string subject = "Reset your password for TheFoody.com";
                string body = "You link: " + resetLink;
                string from = "abcd123@gmail.com";
                string to = resetpasswordmodel.Email;

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(from, to);
                message.Subject = subject;
                message.Body = body;
                SmtpClient client = new SmtpClient();

                // Attempt to send the email
                try
                {
                    client.Send(message);
                }
                catch (Exception e)
                {
                    ModelState.AddModelError("", "Issue sending email: " + e.Message);
                }
            }
            else // Email not found
            {
                ModelState.AddModelError("", "No user found by that email.");
            }
        }
        return View(resetpasswordmodel);
    }

这是我在web.config中的connectionString

<add name="TheFoodyContext" connectionString="metadata=res://*/TheFoodyModel.csdl|res://*/TheFoodyModel.ssdl|res://*/TheFoodyModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-NJOQTOK;initial catalog=TheFoody;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />

我对这个环境很陌生,任何人都可以帮助我吗?

0 个答案:

没有答案