Skip to content

Instantly share code, notes, and snippets.

View rsingh85's full-sized avatar

Ravi Singh rsingh85

View GitHub Profile
@rsingh85
rsingh85 / gist:b3d8aa79470a41fa111f7a6d54c2671f
Created August 3, 2023 11:26
Open Redirect Vulnerability - Return URL Validator
private bool IsValidReturnUrl(string returnUrl, HostString host)
{
if (Uri.TryCreate(returnUrl, UriKind.RelativeOrAbsolute, out var uri))
{
// Check if the URL is absolute and has the same host as the current request.
if (uri.IsAbsoluteUri && uri.Host.Equals(host.Host, StringComparison.OrdinalIgnoreCase))
{
return true;
}
@rsingh85
rsingh85 / gist:1ec488740e900361398ac719c7f5a325
Created May 25, 2023 12:25
MyApplicationExceptionTests.cs
public class MyApplicationExceptionTests
{
[Fact]
public void Constructor_SerializationInfoAndStreamingContext_SetProperties()
{
// Arrange
var message = "Test exception message";
var innerException = new Exception("Inner exception message");
// Create SerializationInfo and StreamingContext objects
@rsingh85
rsingh85 / CustomAuthenticationHandlerTests.cs
Created May 25, 2023 09:49
Unit tests for a custom auth handler
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Moq;
using System.Security.Claims;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Xunit;
private InputBinding CreateMouseClickInputBinding(Cell cell)
{
InputBinding cellTextBlockInputBinding = new InputBinding(
generationViewModel.ToggleCellLifeCommand,
new MouseGesture(MouseAction.LeftClick)
);
cellTextBlockInputBinding.CommandParameter =
string.Format("{0},{1}", cell.Row, cell.Column);
return cellTextBlockInputBinding;
<Button DockPanel.Dock="Top" Command="{Binding EvolveCommand}">Evolve</Button>
<Button DockPanel.Dock="Top" Command="{Binding ResetCommand}" Width="Auto">Reset</Button>
public RelayCommand<object> EvolveCommand { get; private set; }
public RelayCommand<object> ResetCommand { get; private set; }
public RelayCommand<string> ToggleCellLifeCommand { get; private set; }
public class LifeToColourConverter : IValueConverter
{
public SolidColorBrush AliveColour { get; private set; }
public SolidColorBrush DeadColour { get; private set; }
public LifeToColourConverter(SolidColorBrush aliveColour,
SolidColorBrush deadColour)
{
AliveColour = aliveColour;
DeadColour = deadColour;
public class Cell : ObservableBase
{
public int Row { get; private set; }
public int Column { get; private set; }
private bool alive;
public bool Alive
{
get { return alive; }
set
public class ObservableBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string memberName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(
this,
private Binding CreateCellAliveBinding()
{
return new Binding
{
Path = new PropertyPath("Alive"),
Mode = BindingMode.TwoWay,
Converter = new LifeToColourConverter(
aliveColour: Brushes.Black,
deadColour: Brushes.White
)