【问题标题】:FSharp Suave - How to set cookies in the response?FSharp Suave - 如何在响应中设置 cookie?
【发布时间】:2022-01-04 15:27:22
【问题描述】:

我在 Suave 中配置了一个简单的 HTTP 服务器,如下所示。

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

在这里,我有一个登录端点,它将生成一个随机会话 id,我想在带有 HttpOnly 和 Secure 标志的 cookie 中设置该会话 id。

【问题讨论】:

    标签: asp.net cookies f# session-cookies suave


    【解决方案1】:

    在 Suave 中设置 cookie 的典型方法是通过其 cookie 状态存储,如 here 所述:

    statefulForSession
       >=> setSessionValue "sessionId" someLoginLogic
    

    但是,这并不能让您明确设置 cookie 的 securehttpOnly 标志。如果您想这样做,我认为您可以像这样手动进行:

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

    【讨论】:

      猜你喜欢
      • 2023-03-11
      • 2016-02-04
      • 1970-01-01
      • 1970-01-01
      • 2012-03-25
      • 1970-01-01
      • 2021-06-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多