SecureCSharpEval 25.2.28.2
dotnet add package SecureCSharpEval --version 25.2.28.2
NuGet\Install-Package SecureCSharpEval -Version 25.2.28.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="SecureCSharpEval" Version="25.2.28.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SecureCSharpEval" Version="25.2.28.2" />
<PackageReference Include="SecureCSharpEval" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add SecureCSharpEval --version 25.2.28.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: SecureCSharpEval, 25.2.28.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#addin nuget:?package=SecureCSharpEval&version=25.2.28.2
#tool nuget:?package=SecureCSharpEval&version=25.2.28.2
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
SecureCSharpEval
SecureCSharpEval is a secure and sandboxed C# script execution library that allows users to evaluate C# code dynamically while ensuring safety and preventing malicious activities.
Features
- Execute C# scripts dynamically with controlled parameters.
- Prevents unsafe operations such as file access, process execution, and system manipulation.
- Configurable execution timeout and memory limits.
- Optimized for .NET – Fully compatible with .NET 6/7/8/9.
- Optional shell execution support (DISABLED by default for security).
Installation
You can install this library via NuGet Package Manager:
Install-Package SecureCSharpEval
Usage
- Using Conditional Statements, Loops and the new method
class Program
{
private static string checkPrime = @"
static bool IsPrime(int n)
{
if (n < 2) return false;
for (int i = 2; i * i <= n; i++)
{
if (n % i == 0) return false;
}
return true;
};
return IsPrime(number);
";
static async Task Main(string[] args)
{
var n = 4;
var evaluator = new ScriptEvaluator();
Console.WriteLine("Before: {0}",n);
var parameters = new Dictionary<string, object>
{
{ "number", n },
};
var result = await evaluator.EvaluateAsync(checkPrime, parameters);
if (result.HasError)
{
Console.WriteLine($"Error: {result.ErrorMessage}");
}
else
{
Console.WriteLine($"Is Prime: {result.Result}");
Console.WriteLine($"Execution Time: {result.ExecutionTimeMs}ms");
}
Console.ReadKey();
}
}
Configuring Security Options
ScriptEvaluator(new SecurityConfiguration()
{
TimeoutMs = 3000, // Increase timeout to 3 seconds
BlockedKeywords = new HashSet<string>{}, // optional
BlockedNamespaces = new HashSet<string>{}; // optional
}
Security Configuration Default
- AllowedNamespaces: [
System,
System.Collections.Generic,
System.Linq,
System.Text,
System.Threading.Tasks ]
- BlockedNamespaces: [
System.IO,
System.Net,
System.Reflection,
System.Diagnostics,
System.Runtime,
Microsoft.Win32]
- BlockedKeywords: [
unsafe,
fixed,
stackalloc,
Process,
File,
Directory,
Registry,
Socket,
WebClient,
HttpClient]
- TimeoutMs: 5000ms (5 seconds)
- Execution time limit
- Prevent remote command execution
- Prevent remote command execution
Security Considerations
- By default, execution is sandboxed and restricted from running system commands.
- Avoid allowing user-provided scripts without proper review.
Contact
For any questions, feel free to contact me or create an issue in the repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net6.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net7.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net8.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
-
net9.0
- Microsoft.CodeAnalysis.CSharp (>= 4.13.0)
- Microsoft.CodeAnalysis.CSharp.Scripting (>= 4.13.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.