2017-10-16 106 views
0

我正在使用ASP.NET身份核心進行身份驗證。我有一個帶登錄路由的AngularJS SPA(UI-Router)。我的用戶正在路由到一個轉義的URL而不是真正的URL。如何爲Identity Core LoginPath設置特殊字符?

我的登錄路徑設置爲「/#!/ login」,但是我的用戶被路由到:「/%23!/ login」,在瀏覽器中導致401。

我已經嘗試使用System.Uri.EscapeDataString,System.Uri.EscapeUriString,並沒有逃脫,沒有運氣。

Startup.cs

services.AddAuthentication() 
.AddCookie(cookie => 
{ 
    cookie.LoginPath = System.Uri.EscapeDataString("/#!/login"); 
}) 

AngularJS路線

.state('login', { 
     url: 'login', 
     views: { 
      '': { templateUrl: './Home/login.html', controller: "loginController" } 
     } 
    }) 

我已確認該服務器產生與以下位置302響應: 「http://localhost:63939/%23!/login?ReturnUrl=%2Fadministrator」。所以服務器肯定是逃避了「#!」這不是瀏覽器正在做的事情。

+0

您是否啓用HTML 5模式? –

+0

這在角度方面不是問題。由於「/%23!/ login」不存在,瀏覽器正在獲取401。 – Bloodhound

+0

試試這個'url:'#/ login''而不是'url:'login'' –

回答

0

我唯一能解決的就是在OnRedirectToLogin cookie身份驗證事件中使用RedirectUri。有一個更好的方法。

cookie.Events = new CookieAuthenticationEvents() 
{ 
    OnRedirectToLogin = context => 
    { 
context.Response.Redirect(System.Uri.UnescapeDataString(context.RedirectUri)); 
     return Task.FromResult(0); 
    } 
};