C# 如何将输入的用户名存储到数据库表中

C# 如何将输入的用户名存储到数据库表中,c#,sql-server,C#,Sql Server,我试图将登录期间输入的用户名存储在登录详细信息表中。我正在跟踪试图登录应用程序的用户。我的代码在conn.Open崩溃了。请帮我做这个。提前谢谢 以下是我在Homecontroller中编写的代码: public ActionResult Login(LoginViewModel model, string returnUrl) { if (!ModelState.IsValid) { retu

我试图将登录期间输入的用户名存储在登录详细信息表中。我正在跟踪试图登录应用程序的用户。我的代码在conn.Open崩溃了。请帮我做这个。提前谢谢

以下是我在Homecontroller中编写的代码:

public  ActionResult Login(LoginViewModel model, string returnUrl)
        {


            if (!ModelState.IsValid)
            {
                return View(model);
            }                     
            var wwid = ActiveDirectoryUserHelper.returnWWid(model.UserName, model.UserName.Substring(model.UserName.IndexOf("\\")+1), model.Password);
            if(wwid==null)
            {
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
            }
            else
            {
                Session["UserId"] = model.UserName.Substring(model.UserName.IndexOf("\\") + 1); 
                Session["UserPassword"] = model.Password;
                ViewBag.userId = Crypto.EncryptData(model.UserName.Substring(model.UserName.IndexOf("\\") + 1));
                ViewBag.pwd = Crypto.EncryptData(model.Password);
            }

            SqlConnection conn = new SqlConnection(@"data source = XYZ,1433;initial catalog=Application;integrated security=True");

            string sql = "INSERT INTO LoginDetails (username) values (@username)";
            conn.Open();
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.Add("@username", System.Data.SqlDbType.VarChar, 50).Value = model.UserName;
            //cmd.Parameters["@username"].Value = model.UserName;
            cmd.ExecuteNonQuery();

            return View("Management");

        }

您使用的连接字符串似乎有问题

确保传递的服务器名和数据库名正确

另外,如果要使用Windows身份验证或在数据库中配置任何用户登录,请仔细检查

您还可以检查以下URL以了解有关连接字符串的更多信息:


如果您仍然面临错误,请在此处共享错误详细信息。

将此信息放在代码顶部:

使用System.Web.Configuration; 将其放入Web.Config:

 <connectionStrings >
<add
     name="myConnectionString" 
     connectionString="Server=myServerAddress;Database=myDataBase;User ID=myUsername;Password=myPassword;Trusted_Connection=False;"
     providerName="System.Data.SqlClient"/>
</connectionStrings>

and where you want to setup the connection variable:

SqlConnection con = new SqlConnection(
WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString);

以及要设置连接变量的位置:
SqlConnection con=新的SqlConnection(
WebConfiguration Manager.ConnectionString[“myConnectionString”].ConnectionString);

你粘贴了一些假连接字符串吗?是的,我在这里更改了连接字符串。如果
conn.Open
崩溃,则连接字符串很有可能出错。哦,好的。我给web.config上的连接字符串与我在这里使用的连接字符串相同。还有其他方法吗?Crypto.EncryptData(model.Password)不加密密码,当攻击者获得DB时,他也将获得加密密钥。仅仅使用散列函数是不够的,仅仅添加一个salt对提高安全性几乎没有作用。用随机盐在HMAC上迭代大约100毫秒,并用散列保存盐。使用诸如
ehash
PBKDF2
Bcrypt
等功能或类似功能。关键是让攻击者花费大量时间通过暴力手段查找密码。您的用户应该并期望获得良好的安全性。感谢您提供此解决方案,因为我清楚地了解了sql连接的用法。但我仍然在SqlConnection上得到异常“System.invalidOperationException”,我仍然在conn上得到System.invalidOperationException