gpt4 book ai didi

mysql - 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误(在 Asp.net Mvc 应用程序中)

转载 作者:行者123 更新时间:2023-11-29 00:13:43 25 4
gpt4 key购买 nike

我在这里创建登录页面来检查 validateUSer 但出现异常:在这里我发布了我的完整代码请检查一次...... enter image description here

enter code here

namespace Loginusingmvc.Controllers
{
public class LoginController : Controller
{
//
// GET: /Login/

public ActionResult Index()
{
return View();
}
[HttpGet]
public ActionResult LogIn(string returnUrl = "")
{
if (User.Identity.IsAuthenticated)
{
return LogOut();
}

ViewBag.ReturnUrl = returnUrl;
return View();
}
// [HttpPost]
public bool ValidateUser(string UserID, string Password)
{
if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Password))
return false;

using (ntc_salesEntities context = new ntc_salesEntities())
{
var user = (from u in context.user_master
where String.Compare(u.UserID, UserID, StringComparison.OrdinalIgnoreCase) == 0
&& String.Compare(u.Password, Password, StringComparison.OrdinalIgnoreCase) == 0
select u).FirstOrDefault();

return user != null;
}
}

[HttpPost]
public ActionResult Login(user_master model, string returnUrl = "")
{
if (ModelState.IsValid)
{
if (Membership.ValidateUser(model.UserID,model.Password))
{
FormsAuthentication.RedirectFromLoginPage(model.UserID,false);
return RedirectToAction("Index", "Login");
}
else
{
ModelState.AddModelError("", "Login details are wrong.");
}

}
return View(model);

}


public ActionResult LogOut()
{
FormsAuthentication.SignOut();
return RedirectToAction("Index", "Login");
}




}

路线:

   routes.MapRoute(
name: "Default1",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Login", action = "Login", id =
UrlParameter.Optional }
);

HTML

    @model Loginusingmvc.Models.user_master

@{
ViewBag.Title = "LogIn";
Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>LogIn</h2>


@Html.ValidationSummary(true, "Login Failed, check details")

<div>
@using (Html.BeginForm("Login", "Login", new { returnUrl = Request.QueryString["returnUrl"] }, FormMethod.Post))
{

<fieldset>
<legend>Login Form</legend>
<div class="editor-label">@Html.LabelFor(u=> u.UserID)</div>
<div class="editor-field">@Html.TextBoxFor(u=> u.UserID)
@Html.ValidationMessageFor(u=> u.UserID)
</div>
<div class="editor-label">@Html.LabelFor(u=> u.Password)</div>
<div class="editor-field">@Html.PasswordFor(u=> u.Password)
@Html.ValidationMessageFor(u=> u.Password)
</div>

<input type="submit" value="Log In" name="Login" />

</fieldset>
}
</div>

网络配置

    <connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-Loginusingmvc-20140516104010;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-Loginusingmvc-20140516104010.mdf" providerName="System.Data.SqlClient" />
<add name="LoginEntities" connectionString="metadata=res://*/Models.Login.csdl|res://*/Models.Login.ssdl|res://*/Models.Login.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=206.225.85.212;user id=ntc;password=NTC@123;database=ntc_sales&quot;" providerName="System.Data.EntityClient" />
<add name="ntc_salesEntities" connectionString="metadata=res://*/Models.Model1.csdl|res://*/Models.Model1.ssdl|res://*/Models.Model1.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=;user id=ntc;password=NTC@123;database=ntc_sales&quot;" providerName="System.Data.EntityClient" />
<add name="DAConnection" connectionString="Data Source=206.225.85.212;user id=;database=" providerName="MySql.Data.MySQLClient" />

<membership defaultProvider="mysqlprovider">
<providers>
<clear/>
<add name="mysqlprovider"
type="System.Web.Security.SqlMembershipProvider" connectionStringName="DAConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="true"
requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>

最佳答案

检查你的 ConnectionString。您肯定会遇到此错误,因为应用无法与数据库建立连接。

就您使用的 MySql 而言,我建议您实现自定义成员资格提供程序来控制身份验证的所有方面,并能够调试您的代码。以下是如何进行:http://msdn.microsoft.com/en-us/library/ms366730%28v=VS.85%29.aspx

如果您仍想使用 SqlMembershipProvider,请尝试此操作,希望对您有所帮助:

<membership defaultProvider="MySqlMembershipProvider">
<providers>
<clear/>
<add name="MySqlMembershipProvider"
type="MySql.Web.Security.MySQLMembershipProvider, mysql.web"
connectionStringName="DAConnection"
enablePasswordRetrieval="false"
enablePasswordReset="true"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
passwordFormat="Hashed"
maxInvalidPasswordAttempts="5"
minRequiredPasswordLength="6"
minRequiredNonalphanumericCharacters="0"
passwordAttemptWindow="10"
applicationName="/"
autogenerateschema="true"/>
</providers>
</membership>

在那里找到:http://www.integratedwebsystems.com/2010/02/how-to-setup-and-configure-mysql-membership-provider-6-2-2-porting-to-mono-part-2-of-3/

关于mysql - 建立与 SQL Server 的连接时发生与网络相关或特定于实例的错误(在 Asp.net Mvc 应用程序中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23731668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com