8

我一直試圖阻止重定向,當我從控制器返回一個NotAuthorized IActionResult,但無論我的企圖,NotAuthorized被轉換爲重定向。mvc6未授權的結果重定向而不是

我已經嘗試過here(同樣的問題,使用較舊的測試版框架,我使用1.0.0-rc1-final)。我沒有Notifications命名空間(已在rc1-final中刪除)。

這是我的登錄控制器:

在這個
[HttpPost] 
    [AllowAnonymous] 
    public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null) 
    { 
     if (ModelState.IsValid) 
     { 
      var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false); 
      if (result.Succeeded) 
      { 
       return Ok(model); 
      } 
      if (result.IsLockedOut) 
      { 
       return new HttpStatusCodeResult((int)HttpStatusCode.Forbidden); 
      } 
      else 
      { 
       return HttpUnauthorized(); 
      } 
     } 
     return HttpUnauthorized(); 
    } 

在Startup.cs我曾嘗試變化:

 services.Configure<CookieAuthenticationOptions>(o => 
     { 
      o.LoginPath = PathString.Empty; 
      o.ReturnUrlParameter = PathString.Empty; 
      o.AutomaticChallenge = false; 
     }); 

每次登錄失敗(請忽略了密碼就OK返回)並應導致一個空的401頁面,我得到一個重定向到/帳戶/登錄。這裏有什麼竅門?

+0

您是否正在嘗試在登錄頁面上執行此操作?還是用於REST API認證?如果它適用於API,您可能會看到:http://wildermuth.com/2015/9/10/ASP_NET_5_Identity_and_REST_APIs – drinck

+0

REST API,您的鏈接假定​​存在Notifications命名空間,但它已被刪除。鏈接的解決方案不再有效。 – galmok

回答

9

的解決方案是不直接配置CookieAuthenticationOptions,而是通過IdentityOptions做這樣的:

 services.Configure<IdentityOptions>(o => 
     { 
      o.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() 
      { 
       OnRedirectToLogin = ctx => 
       { 
        if (ctx.Response.StatusCode == (int)HttpStatusCode.Unauthorized) 
        { 
         return Task.FromResult<object>(null); 
        } 
        ctx.Response.Redirect(ctx.RedirectUri); 
        return Task.FromResult<object>(null); 
       } 
      }; 
     }); 
+5

只需使用'o.Cookies.ApplicationCookie.AutomaticChallenge = false;'就足夠了我(我從來不想重定向)。 –

+0

@ PeppeL-G這是我唯一能夠工作的東西。非常感謝。 –

8

從這裏拍攝(肖恩Wildermuth - > ASP.NET 5身份和REST API - >評論「邁赫迪哈納菲」),並測試了郵遞員的API

config.Cookies.ApplicationCookie.Events = new CookieAuthenticationEvents() 
{ 
    OnRedirectToLogin = ctx => 
    { 
     if (ctx.Request.Path.StartsWithSegments("/api") && 
     ctx.Response.StatusCode == 200) 
     { 
      ctx.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 
      return Task.FromResult<object>(null); 
     } 
     else 
     { 
      ctx.Response.Redirect(ctx.RedirectUri); 
      return Task.FromResult<object>(null); 
     } 
    } 
}; 
+0

如何讓它重新定向。我希望上帝能讓我的重定向。它不會這樣做。 – Sam

2

如果您有不應該有一個重定向爲其重定向需要一些網頁和其他網址,看到了這個問題,它使用的默認重定向的解決方案邏輯僅適用於非API網址:

Suppress redirect on API URLs in ASP.NET Core

1

從Identity 2.0, 你需要添加:

using Microsoft.AspNetCore.Authentication.Cookies; 

和ConfigureServices:

services.ConfigureApplicationCookie(options => 
{ 
    options.Events = new CookieAuthenticationEvents 
    { 
     OnRedirectToLogin = (x => 
     { 
      if (x.Request.Path.StartsWithSegments("/api") && x.Response.StatusCode == 200) 
       x.Response.StatusCode = 401; 

      return Task.CompletedTask; 
     }), 
     OnRedirectToAccessDenied = (x => 
     { 
      if (x.Request.Path.StartsWithSegments("/api") && x.Response.StatusCode == 200) 
       x.Response.StatusCode = 403; 

      return Task.CompletedTask; 
     }) 
    }; 
}); 

段檢查當然應調整你的路線。

相關問題