我有一个全新的Visual Studio 2015 FSharp项目(.NET Framework 4.6 / FSharp Core 4.4)。我添加了一个脚本文件和代码:
#r "System.Web.ApplicationServices.dll"
open System.Web
open System.Web.Security
let userName = "Jamie"
let user = Membership.GetUser(userName)
当我运行它时,我得到:
System.UnauthorizedAccessException:访问路径' C:\ Program Files(x86)\ Microsoft SDKs \ F#\ 4.0 \ Framework \ v4.0 \ App_Data'被拒绝。 在System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath) 在System.IO.Directory.InternalCreateDirectory(String fullPath,String path,Object dirSecurityObj,Boolean checkHost) 在System.IO.Directory.InternalCreateDirectoryHelper(String path,Boolean checkHost) 在System.IO.Directory.CreateDirectory(String path) 在System.Web.DataAccess.SqlConnectionHelper.CreateMdfFile(String fullFileName,String dataDir,String connectionString) 在System.Web.DataAccess.SqlConnectionHelper.EnsureDBFile(String connectionString) 在System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString,Boolean revertImpersonation) 在System.Web.Security.SqlMembershipProvider.GetUser(String username,Boolean userIsOnline) 在System.Web.Security.Membership.GetUser(String username,Boolean userIsOnline)
我无法相信我必须在我的机器上安装Sql Server等等才能获得当前的Windows用户。我使用了错误的命名空间吗?
修改1
我发现这篇文章(底部答案) Creating local user account c# and .NET 2.0
我转换为F#
#r "System.DirectoryServices.dll"
open System
open System.DirectoryServices
let localMachine = new DirectoryEntry("WinNT://localhost")
let entries = localMachine.Children
let user = entries.Add("NewUser","user")
user.Properties.["FullName"].Add("Local User") |> ignore
user.Invoke("SetPassword","P@ssw0rd")
user.Invoke("Put",[|box "UserFlags"; box 0x10000|])
user.CommitChanges()
但我得到了
System.UnauthorizedAccessException: Access is denied.
at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo()
我可以进入计算机管理并添加用户,因此我知道我的登录用户拥有权限。当我调用WindowsIdentity.GetCurrent()时,我确认它是我的登录用户
fsi是否在不同的上下文中运行?