简体   繁体   中英

FSharp Suave - How to set cookies in the response?

I have a simple HTTP server configured in Suave as follows.

open System
open Suave
open Suave.Operators
open Suave.Filters
open Suave.Successful

[<EntryPoint>]
let main argv =
    
    let httpPort = 9000

    let httpConfig = {
        defaultConfig with bindings = [ HttpBinding.createSimple HTTP "0.0.0.0" httpPort ]
    }

    let httpApp = 
        choose

            [ POST >=> choose
        
                [ 
                    path "/login" >=> 
                        request (fun ctx -> 
                            let sessionId = someLoginLogic
                        ) 
                ]
        
            ]

    startWebServer httpConfig httpApp

Here, I have a login endpoint that will generate a random session id and I want to set that session id in cookies with HttpOnly and Secure flags.

The typical way to set cookies in Suave is via its cookie state store, as documented here :

statefulForSession
   >=> setSessionValue "sessionId" someLoginLogic

However, that doesn't give you a way to explicitly set the cookie's secure and httpOnly flags. If you want to do that, I think you can do it manually like this:

statefulForSession
  >=> Suave.Cookie.setCookie (
    HttpCookie.create "sessionId" someLoginLogic None None None true true None)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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