重定向不包括应用程序名称,并且身份验证无法正常工作

时间:2019-09-21 03:05:18

标签: c# asp.net asp.net-mvc iis

我正在尝试实现“管理员”登录,以便我的用户可以自己添加内容,而我不必担心陌生人会以某种方式路由到“创建/编辑/删除操作”。我将用户名和密码放在Web.config文件中进行身份验证,并且在Visual Studio中一切正常。但是,当我将应用程序发布到IIS时,身份验证似乎失败了,并且当重定向回“登录”操作时,我得到了404 Not Found Error。重定向或返回视图时,路由不包含应用程序名称{application name}/Authentication/Login而是仅返回Authentication/Login,但是当我尝试访问其中一个创建操作时,它将成功重定向,这使我发疯非常感谢您的帮助。

RouteConfig.cs

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

AuthenticationController.cs

namespace ShipleySwine.Controllers
{
    public class AuthenticationController : Controller
    {
        // GET: Authentication
        public ActionResult Login()
        {
            ViewBag.Message = TempData["Message"];
            return View();
        }

        [HttpPost]
        public ActionResult Login(AuthenticationViewModel vm)
        {
            //if (ModelState.IsValid)
            //{
            //    if (vm.user.userName == System.Configuration.ConfigurationManager.AppSettings["Admin"].ToString() && vm.user.password == System.Configuration.ConfigurationManager.AppSettings["adminPass"].ToString())
            //    {
            //        Session["Authentication"] = "Success";
            //        return RedirectToAction("Index", "Admin");
            //    }
            //    else
            //    {
            //        TempData["Message"] = "Login failed, Incorrect username/password";
            //        return Redirect("/ShipleySwine/Authentication/Login");
            //        //return RedirectToAction("Login", "Authentication");
            //    }
            //}
            //else
            //{
            //    //return RedirectToAction("Login", "Authentication");
            //    return Redirect("/ShipleySwine/Authentication/Login");
            //}

            if (ModelState.IsValid)
            {
                if (vm.user.userName == System.Configuration.ConfigurationManager.AppSettings["Admin"].ToString() && vm.user.password == System.Configuration.ConfigurationManager.AppSettings["adminPass"].ToString())
                {
                    Session["Authentication"] = "Success";
                    return RedirectToAction("Index", "Admin");
                }
                else
                {
                    TempData["Message"] = "Login failed, Incorrect username/password";
                    ViewBag.Message = "Login failed, Incorrect username/password";
                    return View();
                }
            }
            else
            {
                return View();
            }
        }
    }
}

Login.cshtml

@model ShipleySwine.ViewModels.AuthenticationViewModel
@{
    ViewBag.Title = "Login";
    Layout = "~/Views/Shared/_Layout.cshtml";

}

<div class="row">
    <div class="col-4">
    </div>
    <div style="padding: 50px; margin-top: 25px;" class="col-4 backgroundcolor rounded">
        <h2 class="text-center">Login</h2>
        <form action="/Authentication/Login" method="post">
            <div class="form-group">
                <label for="exampleInputEmail1">Username</label>
                <input name="user.userName" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Username">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">Password</label>
                <input name="user.password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
            </div>
            @if (ViewBag.Message != null)
            {
                <p class="text-danger font-weight-bold">@ViewBag.Message</p>
            }
            <div class="text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>
        </form>
    </div>
    <div class="col-4">

    </div>
</div>


Web.config

<appSettings>
    <add key="Admin" value="{username}"/>
    <add key ="adminPass" value="{password}"/>
</appSettings>

创建操作成功重定向

public ActionResult Create()
        {
            if(Session["Authentication"] != null)
            {
                if(Session["Authentication"].ToString() != "Success")
                {
                    return RedirectToAction("Login", "Authentication");
                }
                else
                {
                    ViewBag.BoarId = db.Boars.Max(boarid => boarid.Boar_Id) + 1;
                    return View();
                }
            }
            else
            {
                return RedirectToAction("Login", "Authentication");
            }

        }

2 个答案:

答案 0 :(得分:0)

显然我没有给自己足够的时间...

仔细观察后才知道。我将动作硬编码在Login.cshtml

<form action="/Authentication/Login" method="post">

这在Localhost上运行的Visual Studio中很好用,但是当发布到IIS时,该路由不存在。实际上,它不是重定向的,因为表单没有成功传递给控制器​​

更正Login.cshtml

<div class="row">
    <div class="col-4">
    </div>
    <div style="padding: 50px; margin-top: 25px;" class="col-4 backgroundcolor rounded">
        <h2 class="text-center">Login</h2>
        @using (@Html.BeginForm("Login", "Authentication", FormMethod.Post))
        {

            <div class="form-group">
                <label for="exampleInputEmail1">Username</label>
                <input name="user.userName" type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Enter Username">
            </div>
            <div class="form-group">
                <label for="exampleInputPassword1">Password</label>
                <input name="user.password" type="password" class="form-control" id="exampleInputPassword1" placeholder="Password">
            </div>
            if (ViewBag.Message != null)
            {
                <p class="text-danger font-weight-bold">@ViewBag.Message</p>
            }
            <div class="text-center">
                <button type="submit" class="btn btn-primary">Submit</button>
            </div>

        }
    </div>

重要的更改是标记帮助器@using (@Html.BeginForm("Login", "Authentication", FormMethod.Post))

答案 1 :(得分:0)

只需将您的表格更改为此:

<form action="@Url.Action("Login")" method="post">
相关问题