2017-10-15 15 views
1

我正在将Wcf REST服务用于Angular JS应用程序。我正在根据用户名和密码创建userlogin网站。我的Wcf服务获得布尔方法返回true或false。但问题是,我提供的输入字段中提供的用户名信息,我的脚本文件代码始终回复真实。Angular JS脚本应用程序始终使用Wcf服务重新执行

这里是我的接口..

[OperationContract] 
     [WebInvoke(Method = "GET", 
     RequestFormat = WebMessageFormat.Json, 
     ResponseFormat = WebMessageFormat.Json, 
     //BodyStyle = WebMessageBodyStyle.WrappedRequest, 
     UriTemplate = "/AuthenticateUser")] 
     bool AuthenticateUser(UserLogin userLogin); 

下面是接口的实现..

public bool AuthenticateUser(UserLogin userLogin) 
     { 

      string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 

      using (SqlConnection con = new SqlConnection(CS)) 
      { 
       SqlCommand cmd = new SqlCommand("spAuthenticateUser", con); 
       cmd.CommandType = CommandType.StoredProcedure; 
       string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1"); 
       SqlParameter paramUsername = new SqlParameter("@UserName", userLogin.Username); 
       SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword); 

       cmd.Parameters.Add(paramUsername); 
       cmd.Parameters.Add(paramPassword); 

       con.Open(); 
       SqlDataReader rdr = cmd.ExecuteReader(); 
       while (rdr.Read()) 
       { 
        int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]); 
        if (Convert.ToBoolean(rdr["AccountLocked"])) 
        { 
         return false; 
        } 
        else if (RetryAttempts > 0) 
        { 
         int AttemptsLeft = (4 - RetryAttempts); 
         return true; 

        } 

        else if (Convert.ToBoolean(rdr["Authenticated"])) 
        { 
         return true; 
        } 


       } 
       return true; 


      } 
      } 

这里是我的脚本文件代码..

///// <reference path="../angular.min.js" /> 



var app = angular.module("WebClientModule", []) 

    .controller('Web_Client_Controller', ["$scope", 'myService', function ($scope, myService) { 

     $scope.OperType = 1; 

     //1 Mean New Entry 

     //To Clear all input controls. 
     function ClearModels() { 
      $scope.OperType = 1; 
      $scope.Username = ""; 
      $scope.Password = ""; 

     } 
     $scope.login = function() { 
      var User = { 
       Username: $scope.Username, 
       Password: $scope.Password, 
      }; 
      myService.AuthenticateUser(User).then(function (pl) { 
       console.log(pl.data) 
       if (pl.data) { 
        $scope.msg = "Password or username is correct !" 
       } 
       else { 
        $scope.msg = "Password Incorrect !"; 
        console.log("Some error Occured" + err); 
       } 
       }, function (err) { 
       $scope.msg = "Password or username is Incorrect !"; 
       console.log("Some error Occured" + err); 
      }); 
     }; 



    }]); 



app.service("myService", function ($http) { 



    this.AuthenticateUser = function (User) { 
     return $http.post("http://localhost:52098/HalifaxIISService.svc/AuthenticateUser", JSON.stringify(User)); 
    } 
}) 

这里是当我运行应用程序时输出..

Click here to see the result

请提供您的宝贵意见,以纠正此错误。

回答

1

这是因为你总是从方法返回true,只是删除最后一行,介绍了一个变量和结果分配给该变量并返回它,

public bool AuthenticateUser(UserLogin userLogin) 
{ 
    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; 
    using (SqlConnection con = new SqlConnection(CS)) 
    { 
     var result = false; 
     SqlCommand cmd = new SqlCommand("spAuthenticateUser", con); 
     cmd.CommandType = CommandType.StoredProcedure; 
     string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1"); 
     SqlParameter paramUsername = new SqlParameter("@UserName", userLogin.Username); 
     SqlParameter paramPassword = new SqlParameter("@Password", encryptedpassword); 
     cmd.Parameters.Add(paramUsername); 
     cmd.Parameters.Add(paramPassword); 
     con.Open(); 
     SqlDataReader rdr = cmd.ExecuteReader(); 
     while (rdr.Read()) 
     { 
      int RetryAttempts = Convert.ToInt32(rdr["RetryAttempts"]); 
      if (Convert.ToBoolean(rdr["AccountLocked"])) 
      { 
       result = false; 
      } 
      else if (RetryAttempts > 0) 
      { 
       int AttemptsLeft = (4 - RetryAttempts); 
       result = true; 

      } 

      else if (Convert.ToBoolean(rdr["Authenticated"])) 
      { 
       result = true; 
      } 

     } 
     return result; 
    } 
} 
+0

有一切就OK了。在脚本文件?? – Mohammad

+0

@Mohammad您是否按照提及的方式尝试 – Sajeetharan

+0

仍然会返回true并始终显示此消息... $ scope.msg =“密码或用户名正确无误!” – Mohammad

相关问题