简体   繁体   English

使用数据库登录asp.net

[英]asp.net login with database

I have a big problem with Login and database. 我的登录名和数据库有很大问题。 I was searching a lot but couldn't find a solution. 我进行了很多搜索,但找不到解决方案。 First of all I'm trying to get row from my DB. 首先,我试图从数据库中获取行。 When everything is true, I want to redirect. 当一切都正确时,我想重定向。 But when I'm writing my login and password, after around 30 sec I receive "A network-related or instance-specific error occurred while establishing a connection to SQL Server..." 但是,当我编写登录名和密码时,大约30秒后,我收到“与SQL Server建立连接时发生与网络相关或特定于实例的错误...”

Here is my code 这是我的代码

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Configuration;
    using System.Data;
    using System.Data.SqlClient;

    namespace _29
    {
public partial class Default : System.Web.UI.Page
{  
    private string strcon = WebConfigurationManager.ConnectionStrings["UserConnectionString1"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)

 {
        if ((Session["Check"] != null) && (Convert.ToBoolean(Session["Check"]) == true))
        {

// If User is Authenticated then moved to a main page
            if (User.Identity.IsAuthenticated)
                Response.Redirect("Welcome.aspx");
        }     
}

   protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
    {
        Boolean wynik;
        wynik = false;       

wynik = Authentication(Login1.UserName, Login1.Password);

        if (wynik == true) {
            e.Authenticated = true;


Session["Check"] = true;
        }
        else
        {
            e.Authenticated = false;



        }
    }
   private bool Authentication(string p1, string p2)
   {



       SqlConnection con = new SqlConnection(strcon);
       SqlCommand cmd = new SqlCommand("Select count(UserName) from Users where UserName='" + p1 + "' and Password ='" + p2 + "'", con);


con.Open();
       int result = (int)cmd.ExecuteScalar();
       if (result == 1)
       {


return true;
       }
       else
       {


return false;
       }

   }}



       }

Here is my Web.Config 这是我的Web.Config

<configuration>
    <connectionStrings>
        <add name="UserConnectionString1" connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirect`enter code here`ory|\User.mdf;Integrated 

    Security=True"
                    providerName="System.Data.SqlClient" />
            </connectionStrings>
            <system.web>


<compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>
  <appSettings>


<add key="ValidationSettings:UnobtrusiveValidationMode" value="None" />
  </appSettings>
</configuration>

Trying to get the result by rows but it doesn't work. 尝试按行获取结果,但不起作用。 Please help me :) 请帮我 :)

Your connection string seems to be wrong. 您的连接字符串似乎有误。

It should be like 应该像

Server=.\SQLExpress;AttachDbFilename=|DataDirectory|User.mdf;Database=dbname;
Trusted_Connection=Yes;

Not as you've used in you web.config 不像您在web.config中所使用的那样

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirect`enter codehere`ory|\User.mdf;Integrated Security=True

Always check this website to get the proper connection string. 始终检查此网站以获取正确的连接字符串。 It's very useful. 非常有用

Cheers! 干杯!

I think connection is Ok, because when i use connection on Page_Load, i can recieve information from my database. 我认为连接是可以的,因为当我在Page_Load上使用连接时,我可以从数据库中接收信息。 In this case "result" is 1 because only 1 row exist. 在这种情况下,“结果”为1,因为仅存在1行。 But as you can see, i have to put already strings on my code. 但是正如您所看到的,我必须在代码中已经放入字符串。 I want to use input (login control). 我想使用输入(登录控件)。 THank you 谢谢

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace _29
{

    public partial class Default : System.Web.UI.Page
    {


        private string strcon = WebConfigurationManager.ConnectionStrings["UserConnectionString1"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            string p1 ="david";
            string p2 = "1234a";
            SqlConnection con = new SqlConnection(strcon);
            SqlCommand cmd = new SqlCommand("Select count(UserName) from Users where UserName='" + p1 + "' and Password ='" + p2 + "'", con);
            con.Open();
            int result = (int)cmd.ExecuteScalar();
            Response.Write(result);

        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM