# actix_web::middleware::session - Rust
API documentation for the Rust `session` mod in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, session

# Module actix_web::middleware::session [−] [src]

User sessions.

Actix provides a general solution for session management. The SessionStorage middleware can be used with different backend types to store session data in different backends.

By default, only cookie session backend is implemented. Other backend implementations can be added.

CookieSessionBackend uses cookies as session storage. `CookieSessionBackend` creates sessions which are limited to storing fewer than 4000 bytes of data, as the payload must fit into a single cookie. An internal server error is generated if a session contains more than 4000 bytes.

A cookie may have a security policy of signed or private. Each has a respective `CookieSessionBackend` constructor.

A signed cookie may be viewed but not modified by the client. A private cookie may neither be viewed nor modified by the client.

The constructors take a key as an argument. This is the private key for cookie session - when this value is changed, all session data is lost.

In general, you create a `SessionStorage` middleware and initialize it with specific backend implementation, such as a `CookieSessionBackend`. To access session data, HttpRequest::session() must be used. This method returns a Session object, which allows us to get or set session data.

    use actix_web::{actix, server, App, HttpRequest, Result};
    use actix_web::middleware::session::{RequestSession, SessionStorage, CookieSessionBackend};

    fn index(req: HttpRequest) -> Result<&'static str> {
        // access session data
        if let Some(count) = req.session().get::<i32>("counter")? {
            println!("SESSION value: {}", count);
            req.session().set("counter", count+1)?;
        } else {
            req.session().set("counter", 1)?;
        }

        Ok("Welcome!")
    }

    fn main() {
        actix::System::run(|| {
            server::new(
              || App::new().middleware(
                  SessionStorage::new(          // <- create session middleware
                    CookieSessionBackend::signed(&[0; 32]) // <- create signed cookie session backend
                        .secure(false)
                 )))
                .bind("127.0.0.1:59880").unwrap()
                .start();
        });
    }

## Structs

CookieSession | Session that uses signed cookies as session storage
CookieSessionBackend | Use cookies for session storage.
Session | The high-level interface you use to modify session data.
SessionStorage | Session storage middleware

## Enums

CookieSessionError | Errors that can occur during handling cookie session

## Traits

RequestSession | The helper trait to obtain your session data from a request.

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)