0

我有一個AngularJS和WebApi2和FormAuthentication的項目。 因爲我需要從一個使用會話存儲一些用戶變量的WebForms項目導入一些舊代碼,所以我必須在WebApi中實現Session。Web api 302狀態重定向

在WebConfig我:

`

<authentication mode="Forms"> 
     <forms loginUrl="/index.html" defaultUrl="/Areas/Modeler/modeler.html" name=".ASPXFORMSAUTH" protection="All" cookieless="UseDeviceProfile" slidingExpiration="true" path="/" domain="" requireSSL="false" timeout="600" enableCrossAppRedirects="false"> 
     </forms> 
</authentication> 
<authorization> 
     <allow users="*" /> 
</authorization> 
<machineKey validationKey="xxxxx" decryptionKey="xxxxxx" validation="SHA1" /> 
</system.web> 
<location path="Scripts"> 
    <system.web> 
     <authorization> 
      <allow users="*" /> 
     </authorization> 
    </system.web> 
</location> 
<location path="~/Authenticate"> 
    <system.web> 
     <authorization> 
      <allow users="*" /> 
     </authorization> 
    </system.web> 
</location> 

`

我做在Global.asax.cs中的以下變化來實現會話:

`

public override void Init() 
     { 
      this.PostAuthenticateRequest += Application_PostAuthorizeRequest; 
      base.Init(); 
     } 
protected void Application_Start() 
     { 
      AreaRegistration.RegisterAllAreas(); 
      GlobalConfiguration.Configure(WebApiConfig.Register); 
      GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; 
     } 
protected void Application_PostAuthorizeRequest(object sender, EventArgs e) 
     { 
      var url = HttpContext.Current.Request.AppRelativeCurrentExecutionFilePath; 
      var auth = Context.Request.IsAuthenticated; 
      HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required); 
     }` 

用戶輸入他的名字和密碼後,我會調用一個post方法〜/ Authenticate/Login。

在Chrome瀏覽器的網絡選項卡上,我可以看到未來兩行: 登錄方法:開機自檢,狀態302 登錄方法GET,狀態405 對於第一個電話:

Remote Address:[::1]:52966 Request URL:http://localhost:52966/Authenticate/Login Request Method:POST Status Code:302 Found Response Headers view source Content-Length:166 Date:Thu, 14 May 2015 13:11:24 GMT Location:/(S(tdd41h23pms5lllzyro1hltq))/Authenticate/Login Server:Microsoft-IIS/8.0 X-Powered-By:ASP.NET X-SourceFiles:=?UTF-8?B?RDpcUHJvamVjdH..............=?= Request Headers view source Accept:application/json, text/plain, */* Accept-Encoding:gzip, deflate Accept-Language:en-US,en;q=0.8,ro;q=0.6 Connection:keep-alive Content-Length:44 Content-Type:application/json;charset=UTF-8 Cookie:PHPSESSID=d5djbkgs7ttui073jl04mg6st3; __AntiXsrfToken=97b6e321343944a89ade4acc098305bd; ASP.NET_SessionId=2ggakcj1qxtewgsrh5qvtw40; _session_id=BAh7B0kiD3Nlc3Npb25faW....; .AspNet.ApplicationCookie=GcQUqnFHbPaX...; UserPassword=password; UserName=admin; .ASPXFORMSAUTH=7B15EE3DE... DNT:1 Host:localhost:52966 Origin:http://localhost:52966 Referer:http://localhost:52966/index.html User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Request Payload view source {userName: "admin", userPassword: "password"}

的問題是:爲什麼登錄呼叫被重定向? 在Global.ascx.cs中的Application_PostAuthorizeRequest方法中,ai檢查Context.Request.IsAuthenticated並且爲true。

回答

1

重定向來自配置。您已經配置了loginUrl和defaultUrl。這是分別在認證之前和之後發送用戶的地方。

+0

如果我在Webapi中不使用會話進行更改,則問題不存在。這是在web.config中配置表單身份驗證的方法。 – BogdanIM

+0

你的問題是「爲什麼呼叫登錄被重定向?」。我的答案是因爲你有「index.html」配置爲登錄頁面。因此,當您嘗試訪問/身份驗證/登錄時,如果您尚未通過身份驗證,則會重定向到「index.html」。這可能是會話問題的一個單獨問題。 –

+0

好的。那麼當我不使用會話時爲什麼不會發生同樣的方式?在這種情況下,它的web api方法執行狀態代碼爲200. – BogdanIM