简体   繁体   English

AngularJs 应用程序无法使用 Wcf 服务验证用户登录

[英]AngularJs application unable to authenticate user login with Wcf Service

I am consuming Wcf Service into Angular Js Application .我正在将 Wcf Service 用于 Angular Js Application 。 I have one Boolean function inside the wcf service to accept the username and password.我在 wcf 服务中有一个布尔函数来接受用户名和密码。 I am trying to create user login system by using Angular Js Application but when I enter the correct username and password its does not working according my expectation.我正在尝试使用 Angular Js 应用程序创建用户登录系统,但是当我输入正确的用户名和密码时,它无法按我的预期工作。 There are no errors showing in Google Chrome console windows. Google Chrome 控制台窗口中没有显示错误。

Here is Interface .这是接口。

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

Here is Implementation ..这是实现..

 public bool AuthenticateUser(UserLogin userLogin)
        {
            // ConfigurationManager class is in System.Configuration namespace
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            // SqlConnection is in System.Data.SqlClient namespace
            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand cmd = new SqlCommand("spAuthenticateUser", con);
                cmd.CommandType = CommandType.StoredProcedure;

                //Formsauthentication is in system.web.security
                string encryptedpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(userLogin.Password, "SHA1");

                //sqlparameter is in System.Data namespace
                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);
                        //lblMessage.Text = "Invalid user name and/or password. " +
                        //    AttemptsLeft.ToString() + "attempt(s) left";
                    }
                    else if (Convert.ToBoolean(rdr["Authenticated"]))
                    {
                        return true;
                    }

                }
                return false;
            }
        }

Here is Script code .这是脚本代码。

///// <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,


            };
            if ($scope.OperType === 1) {

                var promisePost = myService.AuthenticateUser(User);
                promisePost.then(function (pl) {
                    $scope.Id = pl.data.Id;
                    window.location.href = "/Welcome/Index";

                    ClearModels();
                }, function (err) {
                    $scope.msg = "Password Incorrect !";
                    console.log("Some error Occured" + err);
                });
            }

        };



    }]);

app.service("myService", function ($http) {
    // Create new record  

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

Here is the HTML code..这是HTML代码..

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>Index</title>
    <script src="~/Scripts/angular.min.js"></script>
    <script src="~/RegistrationScript/LoginScript.js"></script>
</head>
<body>
    <table id="tblContainer" data-ng-controller="Web_Client_Controller">
        <tr>
            <td>

            </td>
        </tr>
        <tr>
            <td>
                <div style="color: red;">{{Message}}</div>
                <table style="border: solid 4px Red; padding: 2px;">

                    <tr>
                        <td></td>
                        <td>
                            <span>Username</span>
                        </td>
                        <td>
                            <input type="text" id="username" data-ng-model="Username" required="" />
                        </td>
                    </tr>
                    <tr>
                        <td></td>
                        <td>
                            <span>Password</span>
                        </td>
                        <td>
                            <input type="password" id="password" required data-ng-model="Password" require="" />
                        </td>
                    </tr>

                    <tr>
                        <td></td>
                        <td></td>
                        <td>
                            <input type="button" id="login" value="Login" data-ng-click="login()" />

                        </td>
                    </tr>
                </table>
            </td>
        </tr>
    </table>
</body>
</html>
<script src="~/RegistrationScript/LoginScript.js"></script>

Here is the screen shot when I run the application.这是我运行应用程序时的屏幕截图。 点击这里查看结果

Any Help or suggestion will he highly appreciated.任何帮助或建议,他将不胜感激。

I ran your example in jsfiddle to see what's going on and I changed this:我在 jsfiddle 中运行了你的例子,看看发生了什么,我改变了这个:

<body ng-app="WebClientModule">

I added the ng-app attribute and now is working properly.我添加了ng-app属性,现在工作正常。

See the full example below:请参阅下面的完整示例:

https://jsfiddle.net/s99t344y/2/ https://jsfiddle.net/s99t344y/2/

您没有在 html 端初始化 angular 模块,您应该添加

data-ng-app="WebClientModule"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM