2017-10-13 84 views
0

我有一個Laravel應用程序和一個通過Passport連接到它的C#應用​​程序。laravel護照:爲什麼相同的代碼可以與Http Get請求一起使用,但不適用於POST?

我做了簡單的功能,使用戶更新自己的密碼是這樣的:

Laravel路由

Route::post('user/updatepassword','[email protected]')->middleware('auth:api'); 

Laravel控制器

public function changepassword(Request $request) 
{ 
    $this->validate($request, [ 'password' => 'required|string|max:255' ]); 

    $newuser = User::find(Auth::user()->id); 
    $newuser->password = bcrypt($request->password); 
    $newuser->save(); 
} 

C#功能

public string UpdatePassword(string password) 
{ 
    var client = new RestClient(APPConnection.ApiRoot + "user/updatepassword"); 
    //// client.Authenticator = new HttpBasicAuthenticator(username, password); 

    var request = new RestRequest("", Method.POST); 
    request.AddParameter("password", password); // adds to POST or URL querystring based on Method 
    request.AddHeader("Authorization", "Bearer " + Current_User.access_token); 

    // execute the request 
    IRestResponse response = client.Execute(request); 
    var content = response.Content; // raw content as string 

    return content; 
} 

此代碼不起作用,它會拋出許多無意義的異常,或者至少我無法弄清楚。 但是,當我更改所有在C#代碼和Laravel結束時使用HTTP GET請求時,它工作正常。

我不確定什麼是錯的。這可能是我傳遞令牌的方式對於GET和POST請求應該是不同的。

這是我得到的答覆的複印件:

{ 
    "message": "", 
    "exception": "Symfony\\Component\\HttpKernel\\Exception\\HttpException", 
    "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", 
    "line": 203, 
    "trace": [ 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Exceptions\\Handler.php", 
     "line": 175, 
     "function": "prepareException", 
     "class": "Illuminate\\Foundation\\Exceptions\\Handler", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplication\\app\\Exceptions\\Handler.php", 
     "line": 51, 
     "function": "render", 
     "class": "Illuminate\\Foundation\\Exceptions\\Handler", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 83, 
     "function": "render", 
     "class": "App\\Exceptions\\Handler", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 55, 
     "function": "handleException", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\View\\Middleware\\ShareErrorsFromSession.php", 
     "line": 49, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\View\\Middleware\\ShareErrorsFromSession", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Session\\Middleware\\StartSession.php", 
     "line": 63, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Session\\Middleware\\StartSession", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse.php", 
     "line": 37, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Cookie\\Middleware\\AddQueuedCookiesToResponse", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Cookie\\Middleware\\EncryptCookies.php", 
     "line": 59, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Cookie\\Middleware\\EncryptCookies", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 102, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", 
     "line": 647, 
     "function": "then", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", 
     "line": 622, 
     "function": "runRouteWithinStack", 
     "class": "Illuminate\\Routing\\Router", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", 
     "line": 588, 
     "function": "runRoute", 
     "class": "Illuminate\\Routing\\Router", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Router.php", 
     "line": 577, 
     "function": "dispatchToRoute", 
     "class": "Illuminate\\Routing\\Router", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", 
     "line": 176, 
     "function": "dispatch", 
     "class": "Illuminate\\Routing\\Router", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 30, 
     "function": "Illuminate\\Foundation\\Http\\{closure}", 
     "class": "Illuminate\\Foundation\\Http\\Kernel", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\fideloper\\proxy\\src\\TrustProxies.php", 
     "line": 56, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Fideloper\\Proxy\\TrustProxies", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", 
     "line": 30, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest.php", 
     "line": 30, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Foundation\\Http\\Middleware\\TransformsRequest", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize.php", 
     "line": 27, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Foundation\\Http\\Middleware\\ValidatePostSize", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode.php", 
     "line": 46, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 149, 
     "function": "handle", 
     "class": "Illuminate\\Foundation\\Http\\Middleware\\CheckForMaintenanceMode", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Routing\\Pipeline.php", 
     "line": 53, 
     "function": "Illuminate\\Pipeline\\{closure}", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Pipeline\\Pipeline.php", 
     "line": 102, 
     "function": "Illuminate\\Routing\\{closure}", 
     "class": "Illuminate\\Routing\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", 
     "line": 151, 
     "function": "then", 
     "class": "Illuminate\\Pipeline\\Pipeline", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplicationendor\\laravel\framework\\src\\Illuminate\\Foundation\\Http\\Kernel.php", 
     "line": 116, 
     "function": "sendRequestThroughRouter", 
     "class": "Illuminate\\Foundation\\Http\\Kernel", 
     "type": "->" 
    }, 
    { 
     "file": "C:\\xampp\\htdocs\\myefapplication\\public\\index.php", 
     "line": 55, 
     "function": "handle", 
     "class": "Illuminate\\Foundation\\Http\\Kernel", 
     "type": "->" 
    } 
    ] 
} 
+0

雖然你沒有顯示錯誤,但我猜這是一個CSRF錯誤。 POST路由默認在Laravel中由CSRF保護,所以您需要傳遞有效的CSRF令牌,或者從CSRF保護中免除更新密碼路由(不推薦)。 – patricus

+0

試試這個'Route :: post('/ user/updatepassword','UserController @ changepassword') - > middleware('auth:api');' – lewis4u

+0

@patricus我添加了我從帖子請求中得到的回覆。我沒有理解任何東西,但你可能會。在laravel文檔的api auth部分中,我沒有讀到有關csrf的任何信息。我認爲令牌是它的替代品。我甚至不知道如何通過第三方應用程序傳遞它。 –

回答

0

我不知道爲什麼你的異常輸出是如此可怕,但你正在運行到一個CSRF令牌匹配異常。

Laravel預計在web中間件中的所有POST路由上默認爲CSRF令牌。 routes/web.php文件中定義的所有路由都放置在web中間件組中。

如果這應該是一個API端點(假設您使用護照,我假設它是這樣),則應該在routes/api.php文件中定義您的路由。通過這樣做,它被放置在api中間件組中,而不是web中間件組中。這將除去CSRF標記限制等。

+0

我以爲放置「 - >中間件('auth:api');」之後的路線會做,但它似乎沒有,我不知道爲什麼。無論如何,我把路線放在api.php文件中,並將發佈的請求改爲「APPConnection.ApiRoot +」api/user/updatepassword「」,這個工作,謝謝。 –

相關問題