登录后的MVC重定向

By simon at 2018-02-07 • 0人收藏 • 18人看过

我有一个帐户控制器用户可以登录,和一个名为管理区域 用户必须被自动化才能看到。当用户登录正确 用户名和密码,它重新导向到同一页面 (... /帐号/登录?RETURNURL =%2FAdmin) AccountControll呃

public class AccountController : Controller
    {
        [AllowAnonymous]
        public ActionResult Login()
        {
            return View();
        }
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Login(LoginModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (model.Username == "User" && model.Password == "Pa$$W0rd")
                {
                    FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                    if (!string.IsNullOrWhiteSpace(returnUrl))
                    {
                        return Redirect(returnUrl);
                    }
                    return RedirectToAction("Index", "Admin", new { area = "Admin"});
                }
                ModelState.AddModelError("", "Brukernavn og/eller passord er feil");
            }
            return View();
        }
}
区域Admin中的AdminController
[Authorize]
public class AdminController : HimmelhoytControllerBase
{
        public ActionResult Index()
        {
            return View();
        }
}
查看登录
@model Himmelhoyt.Models.AccountModels.LoginModel
@{
    ViewBag.Title = "Logg inn";
}
    @using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class = "form-signin" }))
    {
        < text>
            @Html.AntiForgeryToken()

            @Html.LabelFor(m => m.Username, new { @class = "sr-only" }) @Html.EditorFor(m => m.Username, new { htmlAttributes = new { @class = "form-control", placeholder = "Brukernavn", autofocus = "autofocus" } })
            @Html.ValidationMessageFor(m => m.Username, "", new { @class = "bg-danger validationMessage" })

            @Html.LabelFor(m => m.Password, new { @class = "sr-only" }) @Html.EditorFor(m => m.Password, new { htmlAttributes = new { @class = "form-control", placeholder = "Passord", type = "password" } })
            @Html.ValidationMessageFor(m => m.Password, "", new { @class = "bg-danger validationMessage" })
            <br/>
            @Html.EditorFor(x => x.RememberMe@*, new { htmlAttributes = new { @class = "checkbox" } }*@) @Html.LabelFor(m => m.RememberMe)
            @Html.ValidationMessageFor(m => m.RememberMe)
            <br />
            @Html.Submit("Logg på", new { @class = "btn btn-lg btn-primary btn-block" })

            @Html.ValidationSummary(true)
        </text>
    }
在帐户控制器中,返回RedirectToAc(“索引”,“管理员”,新{ area =“Admin”});被执行,但正如我所说,它只重定向到相同的 页。 编辑 Web.config
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
  </configSections>
  <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Himmelhoyt-20140831071527.mdf;Initial Catalog=aspnet-Himmelhoyt-20140831071527;Integrated Security=True" providerName="System.Data.SqlClient" />
    <add name="HimmelhoytDb" connectionString="data source=(localdb)\v11.0;initial catalog=Himmelhoyt;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
  <appSettings>
    <add key="webpages:Version" value="3.0.0.0" />
    <add key="webpages:Enabled" value="false" />
    <add key="ClientValidationEnabled" value="true" />
    <add key="UnobtrusiveJavaScriptEnabled" value="true" />
  </appSettings>
  <system.web>
    <!--<authentication mode="None" />-->
    <authentication mode="Forms">
      <forms loginUrl="/Account/Login" />
    </authentication>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
  <system.webServer>
    <modules>
      <remove name="FormsAuthentication" />
    </modules>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" culture="neutral" publicKeyToken="30ad4fe6b2a6aeed" />
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Optimization" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-1.1.0.0" newVersion="1.1.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security.OAuth" publicKeyToken="31bf3856ad364e35" culture="neutral" />
        <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0" />
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
</configuration>

7 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

尝试从Web.config中删除以下行

<modules>
  <remove name="FormsAuthentication" />
</modules>

2018-02-07   #2

你的这行代码为return RedirectToAction("Index", "Admin", new { area = "Admin"}); 可能会抛出异常。尝试   1.试一试,看看有什么例外。   2。您正在传递值,但您的管理员的索引方法不采取任何参数。检查这个。

2018-02-07   #3

尝试这个: 将此方法从Account控制器移至Admin 控制器:

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (model.Username == "User" && model.Password == "Pa$$W0rd")
            {
                FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);
                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Index", "Admin", new { area = "Admin"});
            }
            ModelState.AddModelError("", "Brukernavn og/eller passord er feil");
        }
        return View();
    }
并改变控制r从您的View中调用:
@using (Html.BeginForm("Login", "Account", FormMethod.Post, new { @class = "form-signin" }))
至:
@using (Html.BeginForm("Login", "Admin", FormMethod.Post, new { @class = "form-signin" }))
然后在您转移到管理控制器的方法中更改一行:
return View();
至:
return Redirect("Index");
看看它是否工作,并适合您的需求

2018-02-07   #4

如果我是正确的,我可以看到你设置SetAuthCookie,如果用户提供 正确的凭据,但您尚未登录。 becau你总是这样的 重定向到登录页面。 我认为你必须登录才能看到管理部分。用你的歌唱在方法登录。

[HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginModel model, string returnUrl)
    {
        if (ModelState.IsValid)
        {
            if (model.Username == "User" && model.Password == "Pa$$W0rd")
            {
                FormsAuthentication.SetAuthCookie(model.Username, model.RememberMe);

                //Sign in code should go here.

                if (!string.IsNullOrWhiteSpace(returnUrl))
                {
                    return Redirect(returnUrl);
                }
                return RedirectToAction("Index", "Admin", new { area = "Admin"});
            }
            ModelState.AddModelError("", "Brukernavn og/eller passord er feil");
        }
        return View();
    }
什么是您在应用程序中使用的身份验证方法?是Asp.net吗? 标识框架工作? 希望这可以帮助。

2018-02-07   #5

尝试从Web.config中删除以下行

<modules>
  <remove name="FormsAuthentication" />
</modules>

2018-02-07   #6

我不知道这是好的,但对于你的问题,我做了这个..

    case SignInStatus.Success:
return RedirectToAction("RedirectLogin", new {ReturnUrl = returnUrl});

 public ActionResult RedirectLogin(string returnUrl)
        {

          return User.IsInRole("Reader") ? RedirectToAction("Index", "Employees") : RedirectToLocal(returnUrl);
                    }
如果你不希望它成为“家”“指数”为德故障,改变这一点
private ActionResult RedirectToLocal(string returnUrl)
        {
            if (Url.IsLocalUrl(returnUrl))
            {
                return Redirect(returnUrl);
            }
            return RedirectToAction("Dashboard", "User");
        }
一切在您的帐户控制器,希望它有帮助。

2018-02-07   #7

public static string securityIsnuul(string id)
{
    agancyEntities db = new agancyEntities();

    if (id == null)
    {
      //// redirect to url??????
    }       
}

登录后方可回帖

Loading...