[英]How to add properties to record?
我想要
像下面
let middleware<'a> handler next (ctx: HttpContext) =
task {
let token = ctx.Request.Headers.["Authorization"].ToString()
match token with
| Bearer token ->
let claims = getClaims token
let! command = ctx.BindJsonAsync<'a>()
// how to add claims to command?
return! handler command next ctx
| _ ->
return! RequestErrors.unauthorized "Bearer" "Commerce" authenticationError next ctx
}
我如何進行第三步?
我可以在這里想到 4 個選項。
您可以將 JWT 授權屬性添加到命令 model 中,並在調用BindJsonAsync
然后填充它時避免綁定它們。 這是我最不喜歡的方法,因為您最終會得到可以由發送 model 的人設置的屬性。
您可以使用匿名記錄向您的記錄類型添加其他屬性。 這種方法您需要知道可能導致問題的命令類型,因為看起來您正在使用通用 model 綁定。 它還使得跨二進制邊界傳遞類型變得更加困難。
type CommandHandler = { Command : string; Entity : int }
let command = { Command = "Update"; Entity = 1 }
let authorisedCommand = {| command with AuthToken = "ey ===" |}
type CommandHandlerModel = { Command : string; Id : int }
type AuthorisedCommandHandlerModel = { Command : string; Id : int }
let commandHandler = { CommandHandlerModel.Command = "Update"; Id = 1 }
let authedCommandHandler = { AuthporosedCommandHandlerModel.Command = "Update"; Id = 1 }
type CommandHandlerModel = { Command : string; Id : int }
type AuthorisedCommandHandlerModel<'a> = { CommandModel : 'a; AuthToken : string }
let! command = ctx.BindJsonAsync<'a>()
let authedCommand = { CommandModel = command; AuthToken = claims }
我覺得這種組合方法是解決問題的一種更慣用的方法,也是我過去使用過的一種方法
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.