简体   繁体   English

c#to f#当第三方lib想要一个byref作为out参数时

[英]c# to f# when 3rd party lib wants a byref as an out parameter

I'm trying to convert this method into F# 我正在尝试将此方法转换为F#

private static bool VerifySignedJwt(ECDsa eCDsa, string token) {
    var tokenHandler = new JwtSecurityTokenHandler();
    var claimsPrincipal = tokenHandler.ValidateToken(token, new TokenValidationParameters {
        ValidIssuer = "me",
        ValidAudience = "you",
        IssuerSigningKey = new ECDsaSecurityKey(eCDsa)
    }, out var parsedToken);
    return claimsPrincipal.Identity.IsAuthenticated;
}

I'm translating this great piece of work by Scott Brady in order to create and validate JWT tokens for a web app, the creation part went without a hitch. 我正在翻译斯科特布拉迪为了创建和验证JWT令牌的一个伟大的工作,为一个网络应用程序,创作部分顺利进行。 On with the Validation part and the reference to a byref required in the JwtSecurityTokenHandler.ValidateToken method has stumped me. 在使用Validation部分并且引用JwtSecurityTokenHandler.ValidateToken方法中所需的byref时,我已经难过了。 https://www.scottbrady91.com/C-Sharp/JWT-Signing-using-ECDSA-in-dotnet-Core https://www.scottbrady91.com/C-Sharp/JWT-Signing-using-ECDSA-in-dotnet-Core

If I follow the Microsoft docs below; 如果我按照下面的Microsoft文档; I get an error saying a “type instantiation involves a byref type. 我得到一个错误,说“类型实例化涉及一个byref类型。 This is not permitted by the rules of Common IL.” When trying to declare a byref as: Common IL的规则不允许这样做。“当试图将byref声明为:

let _h (x: byref<'T>) = ()

https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/byrefs I have read the Microsoft docs on byrefs but am left wondering if this relates to .Net core or F#4.5 or does dotnet core supersede all features including byrefs in F#4.5? https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/byrefs我已经阅读了有关byrefs的Microsoft文档,但我想知道这是否与.Net核心或F#4.5或dotnet核心有关取代所有功能,包括F#4.5中的byrefs? Either way I can't seem to instantiate a SecurityToken at all, the "out var parsedToken" is a SecurityToken type, let alone a byref one to use as an output to this 3rd party library, or I can't seem to declare a byref SecurityToken directly in the call to JwtSecurityTokenHandler.ValidateToken. 无论哪种方式我似乎都无法实例化SecurityToken,“out var parsedToken”是一个SecurityToken类型,更不用说byref一个用作第三方库的输出,或者我似乎无法声明一个byref SecurityToken直接调用JwtSecurityTokenHandler.ValidateToken。

If I use the advice posted in this SO post Using c# delegates with f# functions 如果我使用此SO帖子中发布的建议使用带有f#函数的c#delegates

let function_1 (x:double) (y:double byref) = 
    y <- 6.0

I get the same error “type instantiation involves a byref type. 我得到相同的错误“类型实例化涉及一个byref类型。 This is not permitted by the rules of Common IL.” 普通IL的规则不允许这样做。“

I have used Byrefs before in F# but these were simple integer types and worked without issue. 我之前在F#中使用了Byrefs,但这些都是简单的整数类型并且没有问题。 My project is .Net core 2.1 我的项目是.Net core 2.1

Any help appreciated. 任何帮助赞赏。

Since the out parameter in the ValidateToken() call is the last one, you can simply omit it, and treat it as a second return value: 由于ValidateToken()调用中的out参数是最后一个,因此您可以省略它,并将其视为第二个返回值:

let claimsPrincipal, parsedToken = tokenHandler.ValidateToken (token, tvp)

(Here tvp would be your TokenValidationParameters instance.) (这里tvp将是你TokenValidationParameters实例。)

See also https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/parameters-and-arguments#passing-by-reference for more information. 有关更多信息,另请参阅https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/parameters-and-arguments#passing-by-reference

EDIT: Since you don't seem to be using the second return value, I figure your function could look as follows: 编辑:因为您似乎没有使用第二个返回值,我认为您的函数可能如下所示:

let verifySignedJwt ecdsa token =
    let tokenHandler = JwtSecurityTokenHandler ()
    let tvp = TokenValidationParameters (ValidIssuer = "me",
                                         ValidAudience = "you",
                                         IssuerSigningKey = ECDsaSecurityKey ecdsa)
    let claimsPrincipal, _ = tokenHandler.ValidateToken (token, tvp)
    claimsPrincipal.Identity.IsAuthenticated

An alternative (not necessarily better) would be this: 另一种选择(不一定更好)是这样的:

let mutable parsedToken = initialValue

let claimsPrinciple = tokenHandler.ValidateToken(token, tvp, &parsedToken)

// something with both

The above is useful if you have multiple byref parameters, or you don't have the pattern of a single byref parameter at the very end. 如果您有多个byref参数,或者在最后没有单个byref参数的模式,则上述内容非常有用。

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

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