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

chi*_*rag 0 f#

我在C#中有以下代码.它工作得很好.

WindowsIdentity winId = (WindowsIdentity)HttpContext.Current.User.Identity;
WindowsImpersonationContext ctx = winId.Impersonate();
ctx.Undo();
Run Code Online (Sandbox Code Playgroud)

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

Let winId = HttpContext.Current.User.Identity :> WindowsIdentity //Showing error
Let ctx = winId.Impersonate()
ctx.Undo()
Run Code Online (Sandbox Code Playgroud)

在上面的代码HttpContext.Current.User.Identity是不能投入的WindowsIdentity.请让我知道我该怎么做.

N_A*_*N_A 7

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