登录后重定向管理员用户

时间:2020-04-18 19:28:54

标签: asp.net-mvc asp.net-identity owin

我想将管理员用户重定向到管理控制器中的索引操作。但是当我以管理员身份登录并尝试单击“管理员”页面链接时,出现“状态404”,无法访问。 我的adminPartial视图如下:

@if (Request.IsAuthenticated)
{
   RidesDbContext context = new RidesDbContext();
   UserStore<IdentityUser> userStore = new UserStore<IdentityUser>(context);
   UserManager<IdentityUser, string> userManager = new UserManager<IdentityUser, string>(userStore);


   bool exist = userManager.IsInRole(User.Identity.GetUserId(), "Admin");

   if (exist)
   {
           <ul class="nav navbar-nav">
               <li>
                   @Html.ActionLink("Admin Page", "Index", "Admin" , new { @class="nav-link"})
               </li>
        </ul>
   }
}

它要么导航到默认索引(homePage),要么给我一个错误状态。 管理控制器中的索引操作:

    // GET: Admin
        [Authorize]
        public ActionResult Index()
        {
            return View();
        }

我将不胜感激。谢谢。

编辑: 我的索引视图如下:

@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_LayoutPageDefault.cshtml";
}
<link href="~/Styles/styleHomePage.css" rel="stylesheet" />

<script src="~/Scripts/Animation.js"></script>
<div>
    <h2>RidesApp</h2>


    <div class="mask rgba-gradient d-flex justify-content-center align-items-center">
        <!-- Content -->
        <div class="container">
            <!--Grid row-->
            <div class="row">
                <!--Grid column-->
                <div class="col-md-6 text-white text-center text-md-left mt-xl-5 mb-5 wow fadeInLeft" data-wow-delay="0.3s">
                    <h1 class="h1-responsive font-weight-bold mt-sm-5">Make purchases with our app </h1>
                    <hr class="hr-light">
                    <h6 class="mb-4">
                        Lorem ipsum dolor sit amet, consectetur adipisicing elit. Rem repellendus quasi fuga nesciunt
                        dolorum nulla magnam veniam sapiente, fugiat! Commodi sequi non animi ea dolor molestiae
                        iste.
                    </h6>
                    <button type="button" class="btn btn-outline-light btn-lg">
                        @Html.ActionLink("Sign up to Drive", "DriversRegistration", "Drivers")
                    </button>
                    <button type="button" class="btn btn-outline-light btn-lg">
                        @Html.ActionLink("Sign up to Ride", "RidersRegistration", "Riders")

                    </button>
                </div>
                <!--Grid column-->
                <!--Grid column-->
                <div class="col-md-6 col-xl-5 mt-xl-5 wow fadeInRight" data-wow-delay="0.3s">
                    <img src="https://mdbootstrap.com/img/Mockups/Transparent/Small/admin-new.png" alt="" class="img-fluid">
                </div>
                <!--Grid column-->
            </div>
            <!--Grid row-->
        </div>
        <!-- Content -->
    </div>
</div>
<!--

它使用的是共享布局页面,其中包含其余的导航栏链接。

1 个答案:

答案 0 :(得分:1)

根据docs,没有与您的参数匹配的重载。

尝试将其更改为;

@Html.ActionLink("Admin Page", "Index", "Admin" , new { @class="nav-link"}, new { })

[Authorize]表示任何类型的用户,只要已登录即可。如果将您重定向到登录页面,则表示您根本没有登录。除非您的索引视图是登录页面。