使用angularJs客户端路由的MVC应用程序中的身份验证问题

时间:2016-07-12 05:15:51

标签: angularjs asp.net-mvc

我正在使用MVI应用程序,我正在使用angularjs路由,这里是路由的例子

angularFormsApp.config(["$routeProvider", "$locationProvider",
    function ($routeProvider, $locationProvider) {
        $routeProvider.caseInsensitiveMatch = true;
        $routeProvider
            .when("/account/index", {
                title: "Login",
                templateUrl: window.serverURL+"app/Login/loginTemplate.html",
                controller: "loginController"
            })
            .when("/forgotpasswordform", {
                title: "Forgot Password",
                templateUrl: window.serverURL + "app/Login/forgotPasswordFormtemplate.html",
                controller: "loginController"
            })
            .when("/forgotpasswordconfirmation", {
                title: "Forgot Password",
                templateUrl: window.serverURL + "app/Login/forgotPassConfirmationTemplate.html",
                controller: "loginController"
            })
            .otherwise({ redirectTo: "/account/index" });

    }]);

现在我打开默认登录网址时。例如。登录应用程序后http://localhost/#/account/index转到其他页面。

现在如果我再次打开同一个网址http://localhost/#/account/index虽然我已经登录(已通过身份验证)...我正在使用检查用户是否已经验证是否正在加载角度控制器我正在进行ajax调用检查会话是否有效且有效,然后将用户重定向到其他页面。

但问题是这个过程需要几秒钟,而且正在加载页面然后被重定向。

这是登录控制器的代码

if (window.location.href.indexOf("account/index") !== -1) {
        $scope.load();
    }

   $scope.load = function () {

        var onAuthComplete = function (data) {
            window.hideProgress();
            if (data === "redirect") {
                //window.location.href = baseurl;
                $window.location.assign(baseurl);
            }
            else if (data !== "") {
                window.location.href = baseurl + "Listing";
            }
        };


        window.showProgress();
        DataService.checkAuthentication()
            .then(onAuthComplete, onServerError);
    };

认证方法和mvc控制器如下

var checkAuthentication = function () {
                return $http.post(baseurl + "Account/CheckAuthentication/")
                                .then(function (response) {
                                    return response.data;
                                });
            } 

和控制器代码如下

   [HttpPost]
            [AllowAnonymous]
            [ValidateAntiForgeryHeader]
            public ActionResult CheckAuthentication()
            {
                if (Session["UserRole"] != null && Session["UserFullName"] != null && Session["UserName"] != null &&
                    Session["UserType"] != null && Session["EmployeeId"] != null && Session["AuthToken"] != null)
                {
                    if (Request.Cookies["AuthToken"] != null)
                    {
                        var value = Request.Cookies["AuthToken"].Value;

                        if ((string)Session["AuthToken"] == value)
                        {
                            return GetJsonContentResult("Authenticationed");
                        }
                        AuthenticationManager.SignOut();
                        SessionService.Logout();
                        return GetJsonContentResult("redirect");
                    }
                    AuthenticationManager.SignOut();
                    SessionService.Logout();
                    return GetJsonContentResult("redirect");
                }
                return GetJsonContentResult("");
            }

还有其他方法以更好的方式做到这一点,因为它需要一些时间并且平均时间模板正在加载。

0 个答案:

没有答案
相关问题