无法在F#中将IIdentity转换为WindowsIdentity

时间:2015-10-19 13:53:53

标签: f#

我在C#中有以下代码。它运作良好。

WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = winId.Impersonate();
ctx.Undo();

但是我想在F#中转换这段代码。我写了下面的代码。

Let winId = HttpContext.Current.User.Identity :> WindowsIdentity //Showing error
Let ctx = winId.Impersonate()
ctx.Undo()

在上面的代码HttpContext.Current.User.Identity中无法投射WindowsIdentity。请让我知道我该怎么做。

1 个答案:

答案 0 :(得分:7)

问题是,当您要使用向下转换(:>)运算符时,您正在使用向上转换(:?>)运算符。在编译时检查向上转换,因为您始终可以知道是否可以转换继承层次结构,但是向下转换层次结构可能会失败,因此它有一个单独的运算符,它可能在运行时失败。