执行重定向到MVC中的不需要的操作方法

时间:2017-01-18 09:34:47

标签: asp.net-mvc

我有一个使用表单身份验证的简单MVC项目。当用户单击登录按钮时,控制器中的代码部分被调用:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> OturumAc(LoginViewModel model, string returnUrl)
{
    //OturumAcmaSonuc = ResultOfLogin, sonuc = result
    OturumAcmaSonuc sonuc = null;
    //Kullanici = User
    Kullanici kullanicisi = null;
    //Anasayfa = MainPage
    AnasayfaViewModel anasayfaModel = null;
    PersonelViewModel personelvm = null;

    if (!ModelState.IsValid)
    {
        return View(model);
    }
    try
    {
        personelvm = new PersonelViewModel();

        //AcOturum  = Login
        sonuc = this._personelik.AcOturum(model);

        // OturumAcmaSonucu = CanSheLogin
        if (sonuc.OturumAcmaSonucu)
        {
            FormsAuthentication.SetAuthCookie(sonuc.Kullanicisi.KullaniciAdi, false);
            kullanicisi = sonuc.Kullanicisi;
            //Depola = Store
            this._cache.Depola(CacheDepolamaEnum.Kullanici, kullanicisi);

            anasayfaModel = new AnasayfaViewModel(sonuc.Mesaj, kullanicisi);                
            //Depola = Store
            this._cache.Depola(CacheDepolamaEnum.AnasayfaViewModel, anasayfaModel);
            //Anasayfa = MainPage
            return RedirectToAction("Anasayfa", "Account");

        }
        else
        {
            TempData["Hata"] = SistemMesajlar.BasarisizOturumAcma();
        }
    }
    catch (Exception Hata)
    {
        this._hataik.YazHata(Hata);
    }

    return View(model);     

}

此操作方法应是登录的最终方法。正如您所期望的那样,当登录结果为真时,执行期望分支到&#34; Anasayfa&#34; (MainPage)动作方法,但它分支到&#34;登录&#34;行动方法。这是错误。以下是Web层的web.config的内容:

 <system.web>
<globalization uiCulture="tr-TR" culture="tr-TR" />
<compilation debug="true" targetFramework="4.5.2" />
<authentication mode="Forms">
  <forms loginUrl="/Account/OturumAc" timeout="20" slidingExpiration="true" />
</authentication>
<customErrors defaultRedirect="/Error" mode="On">
</customErrors>
<httpModules>
  <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web" />
</httpModules>

当我写&#34;登录&#34;动作方法如下所述,它分支到Login.cshtml:

    // GET: /Account/Login
    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

当我删除[AllowAnonymous]标签时,它会得到&#34;请求过滤模块查询字符串被配置为太长。&#34;请求的网址是:&#34; http://localhost:29736/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%2525252FLogin%2525253FReturnUrl%2525253D%252525252FAccount%252525252FLogin%252525253FReturnUrl%252525253D%25252525252FAccount%25252525252FLogin%25252525253FReturnUrl%25252525253D%2525252525252FAccount%2525252525252FLogin%2525252525253FReturnUrl%2525252525253D%252525252525252FAccount%252525252525252FLogin%252525252525253FReturnUrl%252525252525253D%25252525252525252FAccount%25252525252525252FLogin%25252525252525253FReturnUrl%25252525252525253D%2525252525252525252FAccount%2525252525252525252FLogin%2525252525252525253FReturnUrl%2525252525252525253D%252525252525252525252FAccount%252525252525252525252FLogin%252525252525252525253FReturnUrl%252525252525252525253D%25252525252525252525252FAccount%25252525252525252525252FLogin%25252525252525252525253FReturnUrl%25252525252525252525253D%2525252525252525252525252FAccount%2525252525252525252525252FLogin%2525252525252525252525253FReturnUrl%2525252525252525252525253D%252525252525252525252525252FAccount%252525252525252525252525252FLogin%252525252525252525252525253FReturnUrl%252525252525252525252525253D%25252525252525252525252525252FAccount%25252525252525252525252525252FLogin%25252525252525252525252525253FReturnUrl%25252525252525252525252525253D%2525252525252525252525252525252FAccount%2525252525252525252525252525252FLogin%2525252525252525252525252525253FReturnUrl%2525252525252525252525252525253D%252525252525252525252525252525252FAccount%252525252525252525252525252525252FLogin%252525252525252525252525252525253FReturnUrl%252525252525252525252525252525253D%25252525252525252525252525252525252FAccount%25252525252525252525252525252525252FLogin%25252525252525252525252525252525253FReturnUrl%25252525252525252525252525252525253D%2525252525252525252525252525252525252FAccount%2525252525252525252525252525252525252FLogin%2525252525252525252525252525252525253FReturnUrl%2525252525252525252525252525252525253D%252525252525252525252525252525252525252FAccount%252525252525252525252525252525252525252FAnasayfa&#34;

我试着找到两个解决方案:

1-)每当用户成功登录时,它应分支到&#34; Anasayfa&#34; (MainPage)动作方法。 1-)执行不得分支到&#34;登录&#34;动作方法,它应该分支出来&#34; OturumAc&#34;行动方法。

我怎样才能克服这些?提前谢谢。

0 个答案:

没有答案