Angular JS Script Application始终使用Wcf Service返回True

时间:2017-10-15 00:19:54

标签: javascript c# angularjs wcf

我在Angular JS Application中使用Wcf REST服务。我正在根据用户名和密码创建用户登录网站。我的Wcf服务获得了布尔方法,返回true或false。但问题是我提供给输入字段的用户名信息,我的脚本文件代码总是返回true。

这是我的界面..

 [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 个答案:

答案 0 :(得分: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;
    }
}