Skip to content

verifyspeed/vs-csharp

Repository files navigation

VerifySpeed C#/.NET Client Library

NuGet License

A powerful and secure .NET client library for integrating VerifySpeed user verification services into your C# applications. Support for multiple verification methods including Telegram, WhatsApp, and SMS.

πŸš€ Features

  • Multiple Verification Methods: Support for Telegram, WhatsApp, and SMS verification
  • Local Token Decryption: Decrypt verification tokens locally for enhanced security and performance
  • Dependency Injection Ready: Seamless integration with .NET Core/5+ dependency injection
  • Async/Await Support: Full async support for all operations
  • Comprehensive Error Handling: Detailed exception types for better error management
  • Cross-Platform: Works on Windows, macOS, and Linux

πŸ“¦ Installation

NuGet Package

dotnet add package VerifySpeed.VSCSharp

Package Manager Console

Install-Package VerifySpeed.VSCSharp

πŸ—οΈ Quick Start

1. Service Registration

.NET 6+

using VSCSharp;

var builder = WebApplication.CreateBuilder(args);

// Register VerifySpeed services
builder.Services.AddVerifySpeed("YOUR_SERVER_KEY");

var app = builder.Build();
app.Run();

.NET 5

using VSCSharp;

public class Startup
{
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddVerifySpeed("YOUR_SERVER_KEY");
    }
}

2. Basic Usage

public class VerificationService
{
    private readonly IVerifySpeedClient _verifySpeedClient;

    public VerificationService(IVerifySpeedClient verifySpeedClient)
    {
        _verifySpeedClient = verifySpeedClient;
    }

    public async Task<DecryptTokenResult> VerifyUserAsync(string token)
    {
        try
        {
            // Decrypt token locally (recommended)
            var result = _verifySpeedClient.DecryptToken(token);
            
            // Or use API verification
            // var result = await _verifySpeedClient.VerifyTokenAsync(token);
            
            // The result contains the user's phone number, verification method, and date
            // result.PhoneNumber - The verified user's phone number
            // result.MethodName - The verification method used (e.g., "telegram-message")
            // result.DateOfVerification - When the verification was completed
            
            return result;
        }
        catch (InvalidVerificationTokenException ex)
        {
            // Handle invalid token
            throw;
        }
    }
}

πŸ“š Documentation

πŸ“– Full Documentation: http://doc.verifyspeed.com/docs/Integrations/backend/sdks/csharpdotnet

The complete documentation covers:

πŸ” Verification Methods

  • Telegram Message: Send verification codes via Telegram
  • Telegram OTP: Receiving OTP code via Telegram
  • WhatsApp Message: Send verification codes via WhatsApp
  • WhatsApp OTP: Receiving OTP code via WhatsApp
  • SMS OTP: Receiving OTP code via SMS

πŸ“‹ Requirements

  • .NET 5.0 or later
  • C# 9.0 or later
  • VerifySpeed Account with server key

πŸ”§ Configuration

Environment Variables

VERIFYSPEED_SERVER_KEY=your_server_key_here

appsettings.json

{
  "VerifySpeed": {
    "ServerKey": "your_server_key_here"
  }
}

πŸ“– Examples

Initialize Verification

public async Task InitializeVerificationAsync()
{
    try
    {
        var clientIp = "192.168.1.1"; // Replace with actual client IP
        var initializationResponse = await _verifySpeedClient.InitializeAsync(clientIp);
        
        foreach (var method in initializationResponse.AvailableMethods)
        {
            Console.WriteLine($"- {method.DisplayName} ({method.MethodName})");
        }
    }
    catch (FailedInitializationException ex)
    {
        Console.WriteLine($"Initialization failed: {ex.Message}");
    }
}

Validate OTP (backend)

After creating an OTP verification, validate the code on your server when the user's app sends it to your backend:

public async Task<ValidateOtpResponse> ValidateOtpAsync(string code, string verificationKey)
{
    try
    {
        var result = await _verifySpeedClient.ValidateOtpAsync(code, verificationKey);

        if (!result.Succeed)
        {
            // Handle OTP_EXPIRED, OTP_INVALID, OTP_ALREADY_VERIFIED via result.ErrorCode
            Console.WriteLine($"OTP validation failed: {result.ErrorMessage} ({result.ErrorCode})");
            return result;
        }

        // result.Token and result.PhoneNumber are set on success
        var details = _verifySpeedClient.DecryptToken(result.Token!);
        return result;
    }
    catch (FailedValidateOtpException ex)
    {
        Console.WriteLine($"Validate OTP request failed: {ex.Message}");
        throw;
    }
}

Create Verification

public async Task<CreatedVerificationResponse> CreateVerificationAsync()
{
    try
    {
        var clientIp = "192.168.1.1"; // Replace with actual client IP
        var verification = await _verifySpeedClient.CreateVerificationAsync(
            methodName: "telegram-message",
            clientIPv4Address: clientIp,
            language: "en"
        );
        
        return verification;
    }
    catch (FailedCreateVerificationException ex)
    {
        Console.WriteLine($"Failed to create verification: {ex.Message}");
        throw;
    }
}

🚨 Exception Handling

The library provides specific exception types for better error handling:

try
{
    var result = _verifySpeedClient.DecryptToken(token);
}
catch (InvalidVerificationTokenException ex)
{
    // Token is null, empty, or has invalid format
    Console.WriteLine($"Invalid token: {ex.Message}");
}
catch (FailedDecryptingVerificationTokenException ex)
{
    // Cryptographic error during decryption
    Console.WriteLine($"Decryption failed: {ex.Message}");
}
catch (FailedVerifyingTokenException ex)
{
    // API verification failed
    Console.WriteLine($"Verification failed: {ex.Message}");
}
catch (FailedValidateOtpException ex)
{
    // validate-otp HTTP request failed
    Console.WriteLine($"Validate OTP failed: {ex.Message}");
}

πŸ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

πŸ†˜ Support

πŸ”— Related Links


Made with ❀️ by the VerifySpeed Team

About

Integration SDK for VerifySpeed API

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages