在asp.net网页中尝试WebSecurity.CreateAccount时遇到错误’提供程序遇到未知错误’

前端之家收集整理的这篇文章主要介绍了在asp.net网页中尝试WebSecurity.CreateAccount时遇到错误’提供程序遇到未知错误’前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是asp.net的新手.我正在尝试使用WebMatrix创建一个简单的登录注册网页.但是当我尝试创建帐户时出现以下错误
The Provider encountered an unknown error.

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.Web.Security.MembershipCreateUserException: The Provider encountered an unknown error.

Source Error: 


Line 32:                 db.Execute("INSERT INTO UserData (Email,Name) VALUES (@0,@1)",email,username);
Line 33:                  
Line 34:                 WebSecurity.CreateAccount(email,password);
Line 35:                 Response.Redirect("Homepage.cshtml");
Line 36:             }


Source File: c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml    Line: 34 

Stack Trace: 


[MembershipCreateUserException: The Provider encountered an unknown error.]
   WebMatrix.WebData.SimpleMembershipProvider.CreateAccount(String userName,String password,Boolean requireConfirmationToken) +1312
   WebMatrix.WebData.WebSecurity.CreateAccount(String userName,Boolean requireConfirmationToken) +31
   ASP._Page_Register_cshtml.Execute() in c:\Users\admin\Documents\My Web Sites\Login\Register.cshtml:34
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
   System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
   System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
   System.Web.WebPages.StartPage.RunPage() +19
   System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
   System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext,TextWriter writer,WebPageRenderingBase startPage) +76
   System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +119

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

任何帮助表示赞赏.谢谢.

解决方法

务必:

>首先创建用户记录
>调用WebMatrix以创建用户帐户.

以下是我的代码,它解决了您遇到的同一问题:

public ActionResult SignUp(SignUpModel model)
    {
        if (ModelState.IsValid)
        {
            using (MorphometryContext context = new MorphometryContext())
            {
                User user = new User
                {
                    Email = model.Email,FirstName = model.FirstName,LastName = model.LastName,Username = model.UserName
                };
                context.Users.Add(user);
                context.SaveChanges();
            }

            // Attempt to register the user
            try
            {
                WebSecurity.CreateAccount(model.UserName,model.Password);
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("",ErrorCodeToString(e.StatusCode));
                return View();
            }

            WebSecurity.Login(model.UserName,model.Password);
            return RedirectToAction("Index","Projects");
        }

此外,您不希望Respoonse.Redirect到.cshtml文件.相反,您应该返回RedirectToAction并传入Action和Controller名称.

原文链接:https://www.f2er.com/aspnet/248657.html

猜你在找的asp.Net相关文章