使用C#网站登录使用现有的SQL Server数据库连接 [英] Website login using C# to connect with existing sql server db

查看:110
本文介绍了使用C#网站登录使用现有的SQL Server数据库连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个品牌的新手...

I'm a brand newbie...

我试图登录到我的网站在VS2010,其中2008例preSS数据库通过asp.net连接到现有的SQL Server,利用C#为code后面。

I am attempting to log on to my website in VS2010, which connects to an existing SQL Server 2008 Express database through asp.net, utilizing C# as the code behind.

下面是我的 login.aspx.cs code:

Here's my login.aspx.cs code:

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

public partial class Login : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    protected void btnlogin_Click(object sender, EventArgs e)
    {
        int Results = 0;

        if (txtUsername.Text != string.Empty && txtPassword.Text != string.Empty)
        {

            Results = Validate_Login(txtUsername.Text.Trim(), txtPassword.Text.Trim());

            if (Results == 1)
            {
                lblMessage.Text = "Login is Good, Send user to another page or enable controls.";
            }
            else
            {
                lblMessage.Text = "Username or Password is incorrect.";
                lblMessage.ForeColor = System.Drawing.Color.Red;
            }
        }
        else
        {
            lblMessage.Text = "Please make sure that your username and password is correct.";
        }
    }

    protected int Validate_Login(String Username, String Password)
    {
        SqlConnection con = new SqlConnection(@"Server=MARIOM-PC\SQLEXPRESS;Database=Logon");

        SqlCommand cmdselect = new SqlCommand();

        cmdselect.CommandType = System.Data.CommandType.StoredProcedure;

        cmdselect.CommandText = "[dbo].[prcLoginv]";

        cmdselect.Parameters.Add("@Username", System.Data.SqlDbType.VarChar, 50).Value = Username;
        cmdselect.Parameters.Add("@Password", System.Data.SqlDbType.VarChar, 50).Value = Password;
        cmdselect.Parameters.Add("@OutRes", System.Data.SqlDbType.Int, 4);
        cmdselect.Parameters["@OutRes"].Direction = System.Data.ParameterDirection.Output;

        cmdselect.Connection = con;

        int Results = 0;

        try
        {
            con.Open();

            cmdselect.ExecuteNonQuery();

            Results = (int)cmdselect.Parameters["@OutRes"].Value;
        }
        catch (SqlException ex)
        {
            lblMessage.Text = ex.Message;
        }
        finally
        {
            cmdselect.Dispose();

            if (con != null)
            {
                con.Close();
            }
        }

        return Results;
    }
}

当我点击我的登录按钮,它带我到我的C#code的后面应通过迭代 btn_Login_Click ,那么 Validate_Login 方法。但它不能正常用正确的信息更新我的登录页面。我总是得到密码不正确的错误。

When I click my Log In button, it takes me to my C# code behind which should iterate through the btn_Login_Click, then the Validate_Login method. But then it does not correctly update my login page with the correct information. I always get the "incorrect password" error.

请帮帮忙!

推荐答案

我会建议你使用Windows窗体身份验证

I would recommend you use Windows Forms Authentication

要窗体身份验证设置为SQL Server数据库

to set up forms authentication to sql server database

转到C:>> >>的Windows Microsoft.Net>框架>>您的版本比如矿山V4.0 >> aspnet_reqsql.exe

go to C:>>Windows>>Microsoft.Net>Framework>>your version example mines v4.0>>aspnet_reqsql.exe

它的向导设置窗体身份验证方案,以特定的数据库。

its a wizard to setup forms authentication scheme to your specific database.

然后在web.config中

then in your web.config

<connectionStrings>
    <add name="ConnectionStringName" connectionString="*********" providerName="System.Data.SqlClient"/>
  </connectionStrings>
  <system.web>
    <roleManager enabled="true"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
      </assemblies>
    </compilation>
    <authentication mode="Forms">
      <forms loginUrl="login.aspx"/>
    </authentication>
    <membership defaultProvider="SqlProvider">
      <providers>
        <clear/>
        <add connectionStringName="ConnectionStringName" applicationName="/" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="true" passwordFormat="Hashed" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" name="SqlProvider" type="System.Web.Security.SqlMembershipProvider"/>
      </providers>
    </membership>
  </system.web>

login.aspx的

login.aspx

只需将Login控件拖放到页面,您可以使用网站管理工具
设置权限和用户,并限制某些文件夹的用户。

just drag and drop Login control to your page you can use Web Site Administration Tool to set rights and user and restrict user from certain folders.

这篇关于使用C#网站登录使用现有的SQL Server数据库连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆