【问题标题】:Angular JS Script Application always return True with Wcf ServiceAngular JS 脚本应用程序始终使用 Wcf 服务返回 True
【发布时间】:2017-10-15 00:19:53
【问题描述】:

我在 Angular JS 应用程序中使用 Wcf REST 服务。我正在根据用户名和密码创建用户登录站点。我的 Wcf 服务得到了布尔方法,它的返回真或假。但问题是我在输入文件中提供的任何用户名信息我的脚本文件代码总是返回 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));
    }
})

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

请提供有价值的反馈以纠正此错误。

【问题讨论】:

    标签: javascript c# angularjs wcf


    【解决方案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;
        }
    } 
    

    【讨论】:

    • 脚本文件中是否一切正常??
    • @Mohammad 你按照上面说的尝试了吗
    • 仍返回 true 并始终显示此消息 ... $scope.msg = "密码或用户名正确!"
    • 不可能,你重启wcf服务了吗
    • 是的,我做到了,但为什么还是一样的结果
    猜你喜欢
    • 1970-01-01
    • 2010-10-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2012-05-12
    相关资源
    最近更新 更多