在asp.net Web应用程序上使用WCF服务应用程序登录

Ra9*_*a91 2 c# sql asp.net passwords login

我正在为一个学校项目开发一个约会网站,我目前正在尝试为它创建一个登录功能.我们不应该使用自动注册和登录功能.

我们与数据库的任何联系都应该通过WCF服务应用程序.我知道如何在不使用WCF的情况下实现它,但我现在需要使用它,我在搜索后无法在Google上找到它.

 public bool login(string UserName, string PassWord, bool isActive = true) {
      try {
           DALDataContext db = new DALDataContext();
           var qry = from m in db.tblUsers
                      where m.userName == UserName && m.password == PassWord && m.isActive == isActive
                      select m;
            if (qry.Count() > 0) {
                return true;
            } else {
                return false;
            }
        }
        catch (Exception) {
            return false;
        }
    }
Run Code Online (Sandbox Code Playgroud)

我就是这样做的,所以如果我在我的Web应用程序中实现它,这应该是这样的:

ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client();
protected void btnLoginUser_Click1(object sender, EventArgs e) {
     try {
          string UserName = txtUserName.Text;
          string PassWord = txtPassWord.Text;
          obj.login(UserName, PassWord);

           if (true) {
               Session["me"] = UserName;
               Response.Redirect("~/MyProfile.aspx");
               }
      }
      catch (Exception){

       }
   }
Run Code Online (Sandbox Code Playgroud)

我已经使用了几个小时,这个注册部分工作......所以我做了一些非常错误的事情.我正在使用Visual Studio 2010和SQL Server 2008 R2.

[解决了]

这就是我解决它的方式

protected void btnLoginUser_Click1(object sender, EventArgs e)
    {
        try
        {
            string UserName = txtUserName.Text;
            string PassWord = txtPassWord.Text;
            bool isActive = true;


            if (obj.login(UserName, PassWord, isActive))
            {
                Session["me"] = UserName;
                Response.Redirect("~/MyProfile.aspx");
            }
            else
            {
                lblErr.Text = "fail";
            }
            }

        catch (Exception)
        {

        }

    }
}
Run Code Online (Sandbox Code Playgroud)

}

Bor*_*ort 5

您忽略了登录方法的返回值:

obj.login(UserName, PassWord); // <-- returns true/false.

if (true) // <-- Why?
{ 
    ...
Run Code Online (Sandbox Code Playgroud)

你的意思是做什么

if (obj.login(UserName, PassWord))
{
     Session["me"] = UserName;
     Response.Redirect("~/MyProfile.aspx");
} ...
Run Code Online (Sandbox Code Playgroud)