使用ASP.NET MVC 5登录后重定向到Angular路由

问题描述 投票:4回答:2

我正在使用Asp.net mvc 5应用程序日志记录,并在AccountController中执行以下日志记录操作:

    // POST: /Account/Login
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
    {

          ## not working, just a trial
        ##returnUrl = "Dashboard#!/requests";

        if (!ModelState.IsValid)
        {
            return View(model);
        }

        // This doesn't count login failures towards account lockout
        // To enable password failures to trigger account lockout, change >to shouldLockout: true

        var result = await > >SignInManager.PasswordSignInAsync(model.UserName, model.Password,

model.RememberMe,shouldLockout:false);

        switch (result)
        {
            case SignInStatus.Success:
                return RedirectToLocal(returnUrl);
            case SignInStatus.LockedOut:
                return View("Lockout");
            case SignInStatus.RequiresVerification:
                return RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe });
            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return View(model);
        }
    }

并且使用以下形式:

<section id="loginForm">
            @using (Html.BeginForm("Login", "Account", new { ReturnUrl = ViewBag.ReturnUrl }, FormMethod.Post, new { @class = "form-horizontal", role = "form" }))
            {
                @Html.AntiForgeryToken()
                <h4>Use a local account to log in.</h4>
                <hr />
                @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                <div class="form-group">
                    @Html.LabelFor(m => m.UserName, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.TextBoxFor(m => m.UserName, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.UserName, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    @Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
                    <div class="col-md-10">
                        @Html.PasswordFor(m => m.Password, new { @class = "form-control" })
                        @Html.ValidationMessageFor(m => m.Password, "", new { @class = "text-danger" })
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <div class="checkbox">
                            @Html.CheckBoxFor(m => m.RememberMe)
                            @Html.LabelFor(m => m.RememberMe)
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <div class="col-md-offset-2 col-md-10">
                        <input type="submit" value="Log in" class="btn btn-default" />
                    </div>
                </div>
                @*<p>
                    @Html.ActionLink("Register as a new user", "Register")
                </p>*@
                @* Enable this once you have account confirmation enabled for password reset functionality
                    <p>
                        @Html.ActionLink("Forgot your password?", "ForgotPassword")
                    </p>*@
            }
        </section>

用户登录后,我希望用户重定向到网址“ Dashboard#!/ requests”其中Dashboard是ASP.NET MVC 5控制器,/ requests是使用HTML5和!的Angular路由。前缀:

 $locationProvider.html5Mode(false).hashPrefix('!'); // This is for Hashbang Mode

app.config(function($ routeProvider,$ locationProvider){$ routeProvider

        .when('/requests', {
            templateUrl: '/Template/Request/Index.html',
            controller: 'requestsController'

        })
        .when('/request/:wf/:requestId', {
            templateUrl: function(params) {
                return '/Template/Request/' + params.wf + '/Open.html';
            },
            controller: 'requestsController'
        });

    $locationProvider.html5Mode(false).hashPrefix('!'); // This is for Hashbang Mode


});

如何将用户重定向到结合了asp.net mvc 5控制器和Angular的URL中?

angularjs asp.net-mvc-5 asp.net-mvc-routing angularjs-routing
2个回答
0
投票

我想没有办法...

经过搜索和搜索……我认为不可能(容易),URL的最后一部分没有到达服务器。

我认为解决方案是在本文中使用html5Mode(true)展示如何:http://www.jlum.ws/post/2014/10/10/handling-routes-between-an-angularjs-application-and-aspnet-mvc-application


0
投票

您可以做到,您只需要组织RouteConfig.cs文件。将用户定向到Home Controller的Index方法,还要删除Home Index View中的所有代码。在您的布局中添加应用程序根目录,您将需要执行以下操作。

    @if (User.Identity.IsAuthenticated)
    {
        <app-root></app-root>
    }
    @RenderBody()

接下来,您需要设置RouteConfig.cs文件来处理此问题。您将需要执行以下操作

        routes.MapRoute(
            name: "logout",
            url: "Account/Logoff",
            defaults: new { controller="Account", action = "Logoff", id = 
            UrlParameter.Optional}
            );

        routes.MapRoute(
            name: "login",
            url: "Account/Login",
            defaults: new { controller = "Account", action = "Login", id = 
            UrlParameter.Optional }
            );

        routes.MapRoute(
          name: "register",
          url: "Account/Register",
          defaults: new { controller = "Account", action = "Register", id = 
          UrlParameter.Optional }
          );

         routes.MapRoute(
            name: "Angular",
            url: "{*url}",
            defaults: new { controller = "Home", action = "Index"}
            );

这将使Asp.Net负责登录,注销和注册用户。

最后,您需要设置app-routing-module.ts文件。只需将路径设置为登录后希望用户访问的组件。这将是基地址或“家庭”地址。

import { HomeComponent } from '{path to home component}';

const routes: Routes = [
  { path: '/', component: HomeComponent }
];

这将使用户登录后到达应用程序的角度。

希望这对某人有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.