简体   繁体   English

Laravel API过期的会话

[英]Laravel API expired session

In Laravel 5.4 I created some API on my routes/web.php. 在Laravel 5.4中,我在route / web.php上创建了一些API。 These API are called by .NET application. 这些API由.NET应用程序调用。 The first called API is the login, after that, I put in session some datas. 第一个调用的API是登录名,之后,我在会话中添加了一些数据。 Before to call other API, I check if the datas are in session, but after the first call (login), when I try to call the second one, the session seems to be flushed/expired. 在调用其他API之前,我会检查数据是否在会话中,但是在第一次调用(登录)后,当我尝试调用第二个API时,会话似乎已刷新/过期。

Here my route code: 这是我的路线代码:

// API
Route::group(["prefix" => "api"], function() {
    // Login
    Route::post("login", "APIController@login");

    Route::group(["middleware" => "isNotAuthenticatedAPI"], function() {
        Route::group(["middleware" => "loginDBUser"], function() {
            // Importazioni
            Route::group(["prefix" => "importazioni"], function() {

                // Utenti
                Route::post("utenti", "APIController@importCustomers");
                // Attività (Abbonamenti)
                Route::post("attivita", "APIController@importSubscriptions");
                // Iscrizioni
                Route::post("iscrizioni", "APIController@importCustomersSubscriptions");
            });

            // Richieste
            Route::group(["prefix" => "richieste"], function() {
                // Ultima iscrizione
                Route::post("ultima_iscrizione/{subscription_external_id?}", "APIController@getLastCustomerSubscription");
            });
        });
    });
});

Middleware isNotAuthenticatedAPI: 中间件isNotAuthenticatedAPI:

public function handle($request, Closure $next) {
        $res = [
            "result" => null,
            "errors" => [
            ]
        ];

        if (!$request->session()->get("user.api")) {
            $res["result"] = false;
            $res["errors"][] = [
                "code" => APIController::WARNING_SESSION_EXPIRED,
                "message" => trans("api.w_session_expired")
            ];
            return response()->json($res);
        }

        return $next($request);
    }

My config/session.php: 我的config / session.php:

'lifetime' => 120,

    'expire_on_close' => false,

I tried to put the route's code into routes/api.php, but the result doesn't change. 我试图将路线的代码放入route / api.php,但结果没有改变。

请在config / session.php生命周期=值中设置属性,以增加会话超时时间,或者通过设置中间件处理程序将此选项设置为其他值,设置为“ config('session.lifetime')* 60; //分钟到小时的转换以解决此问题问题

If you want to continue using Session in API, please make sure that the client (your .NET application) store the cookie and include it in later requests. 如果要继续使用API​​中的Session,请确保客户端(您的.NET应用程序)存储cookie并将其包含在以后的请求中。

A simple way to test if your problem is the cookie, try login your api in browser and call other api after that. 一种简单的方法来测试您的问题是否是cookie,请尝试在浏览器中登录您的api,然后再调用其他api。 If everything work well, then it's the cookie problem. 如果一切正常,那就是cookie问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM