简体   繁体   English

成功消息后如何重定向登录页面

[英]How to redirect login page after successfull message

I am programing a web application, The first page will be the login page, I already did that on the RouteConfig.我正在编写一个 web 应用程序,第一页将是登录页面,我已经在 RouteConfig 上这样做了。 I developed a function that do the checking with the controller, back with successfull message and after should reidrect to my index page.我开发了一个 function 用 controller 进行检查,返回成功消息,之后应该重定向到我的索引页面。 The message is shown but the redirection never passed.显示消息,但重定向从未通过。 I tried to use two separate functions and controllers for login part and redirection part, with ng-submit for all the login form to do the login check and ng-click on the login submit to do the redirection.我尝试对登录部分和重定向部分使用两个单独的函数和控制器,使用 ng-submit 对所有登录表单进行登录检查,并在登录提交上使用 ng-click 进行重定向。 Please help me with that.请帮帮我。

DataController:数据控制器:

public JsonResult UserLogin (LoginData d)
    {
        using (GestReclamationDBEntities dc = new GestReclamationDBEntities())
        {
            var user = dc.Users.Where(a => a.Username.Equals(d.Username) && 
a.Password.Equals(d.Password)).FirstOrDefault();
            return new JsonResult { Data = user, JsonRequestBehavior = JsonRequestBehavior.AllowGet 
};
        }
    }
public ActionResult Index()
    {
        return View();
    }

login-controller.js:登录-controller.js:

(function () {
'use strict';

angular
    .module('app')

    .controller('LoginController', function ($scope, $window, LoginService) {

        $scope.IsLogedIn = false;
        $scope.Message = '';
        $scope.Submitted = false;
        $scope.IsFormValid = false;

        $scope.LoginData = {
            Username: '',
            Password: ''
        };

        //Check is Form Valid or Not
        $scope.$watch('f1.$valid', function (newVal) {
            $scope.IsFormValid = newVal;
        });

        $scope.login = function () {
            $scope.Submitted = true;
            if ($scope.IsFormValid) {
                LoginService.GetUser($scope.LoginData).then(function (d) {
                    if (d.data.Username != null) {
                        var host = $window.location.host;
                        var landingUrl = "/Data/Index";
                        $scope.IsLogedIn = true;
                        $scope.Message = "Successfully login done. Welcome ";
                        $window.location.href = landingUrl;
                    }
                    else {
                        alert('Invalid Credential!');
                    }
                });
            }
        };

    })

    .factory('LoginService', function ($http) {

        var fac = {};
        fac.GetUser = function (d) {
            return $http({
                url: '/Data/UserLogin',
                method: 'POST',
                data: JSON.stringify(d),
                headers: { 'content-type': 'application/json' }
            });
        };
        return fac;

    });

})();

Login.cshtml:登录.cshtml:

<div ng-controller="LoginController">
<div class="login-form">
    <form novalidate name="f1" ng-submit="login()">
        <div style="color:rgb(142,2,2)">{{Message}}</div>
        <div ng-show="!IsLogedIn">
            <h2 class="text-center">Log in</h2>
            <div class="form-group">
                <input type="text" class="form-control" placeholder="Username" ng- 
model="LoginData.Username" name="cUsername" ng-class="Submitted?'ng-dirty':''" required autofocus>
                <span class="error" ng-show="(f1.cUsername.$dirty || Submitted) && 
f1.cUsername.$error.required">Username required</span>
            </div>
            <div class="form-group">
                <input type="password" class="form-control" placeholder="Password" ng- 
model="LoginData.Password" name="cPassword" ng-class="Submitted?'ng-dirty':''" required autofocus>
                <span class="error" ng-show="(f1.cPassword.$dirty || Submitted) && 
f1.cPassword.$error.required">Password required</span>
            </div>
            <div class="form-group">
                <button type="submit" class="btn btn-primary btn-block">Log in</button>
            </div>
        </div>

    </form>
</div>


</div>



@section scripts{

<script src="~/Scripts/app/controllers/login-controller.js"></script>
@*<script src="~/Scripts/app/controllers/access-to-app-controller.js"></script>*@
}

EDIT:编辑:

I tried to work with location.href like this:我尝试像这样使用 location.href:

$("#send").click(function () {
        $.ajax({
            type: 'POST',
            url: '@Url.Action("Login")',
            //dataType: 'json',
            //contentType: dataType,
            data: { Username: $("#Username").val(), Password: 
 $("#Password").val() },
            success: function (result) {
                if (true) {
                    location.href = '/Data/Index';
                } else {
                    location.href = 'Login';
                }
            },
            error: function (result) {
            }
    });

    });

I also add the id="send" to my Login button.我还将 id="send" 添加到我的登录按钮。 Now when I click on login he goes directly yo the index page without verifiying credentials.现在,当我单击登录时,他直接进入索引页面而不验证凭据。

Try to do a location.href ='Controller/Method';尝试做一个 location.href ='Controller/Method';

Do something like that做这样的事情

$.ajax({
                type: 'POST',
                url: '@Url.Action("Login")',
                //dataType: 'json',
                //contentType: dataType,
                data: { Username: $("#Username").val(), Password: $("#Password").val() },
                success: function (result) {
                    if (//anything in result) {
                        location.href = 'Controller1/Index1';
                    } else {
                        location.href = 'Controller2/Index2';
                    }
                },
                error: function (result) {
                }
            });

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

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