Skip to content

Instantly share code, notes, and snippets.

View SteveSandersonMS's full-sized avatar

Steve Sanderson SteveSandersonMS

View GitHub Profile
@SteveSandersonMS
SteveSandersonMS / GridSample.razor
Created March 16, 2023 16:18
Grid with columns in method
@page "/"
@using Microsoft.AspNetCore.Components.QuickGrid
<label>
<input @bind="@showName" type="checkbox" />
Show name
</label>
<QuickGrid Items="@people">
@Columns()
public class RevalidatingAuthenticationStateProvider : AuthenticationStateProvider, IDisposable
{
private static TimeSpan RefreshInterval = TimeSpan.FromMinutes(30);
private readonly CancellationTokenSource _cts;
private ClaimsPrincipal _currentUser;
public RevalidatingAuthenticationStateProvider(SignInManager<IdentityUser> signInManager)
{
_cts = new CancellationTokenSource();
@SteveSandersonMS
SteveSandersonMS / auth.md
Last active September 28, 2022 19:30
Authentication and Authorization

This document describes options and proposals for #4048.

Overall goals

Principles:

  • In Razor Components, enable customers to use as much of ASP.NET Core's existing authentication/authorization/identity feature set as possible. Minimize the invention of new stuff; maximize orthogonality.
  • Provide a programming model that can be consistent across Razor Components (server-side) and Blazor (client-side), so components that use authorization can still be portable across the two, even if app-level Startup.cs logic is completely different for the two runtime environments.

The Razor Components auth feature set will be mostly the same as for MVC/Pages. We'll get the same long-tail features such as integration with 3rd-party social logins, 2FA, etc. The Razor Components template will support the same four auth options during project creation, and the "Scaffold Identity" feature will be usable on existing Razor Components projects.

@SteveSandersonMS
SteveSandersonMS / ITab.cs
Created November 15, 2018 11:33
Blazor tab example
using Microsoft.AspNetCore.Blazor;
public interface ITab
{
RenderFragment ChildContent { get; }
}
public static class EventUtil
{
// The repetition in here is because of the four combinations of handlers (sync/async * with/without arg)
public static Action AsNonRenderingEventHandler(Action callback)
=> new SyncReceiver(callback).Invoke;
public static Action<TValue> AsNonRenderingEventHandler<TValue>(Action<TValue> callback)
=> new SyncReceiver<TValue>(callback).Invoke;
public static Func<Task> AsNonRenderingEventHandler(Func<Task> callback)
=> new AsyncReceiver(callback).Invoke;
public static Func<TValue, Task> AsNonRenderingEventHandler<TValue>(Func<TValue, Task> callback)
@SteveSandersonMS
SteveSandersonMS / Home.cshtml
Created February 20, 2018 10:54
Manually implemented HTTP client
@using StandaloneApp.Util
@(Layout<StandaloneApp.Shared.MainLayout>())
<h1>Hello, world!</h1>
Welcome to your new app.
<button @onclick(DoRequest)>Do request</button>
<div><strong>Response: </strong>@responseText</div>
@functions {
@SteveSandersonMS
SteveSandersonMS / index.js
Created December 6, 2019 14:22
Trivial webpack setup
console.log('Hello, world!');
@page "/"
<h1>Hello, world!</h1>
Welcome to your new app.
<EditForm Model="@_editingPerson" OnValidSubmit="@OnValidSubmit" OnInvalidSubmit="@OnInvalidSubmit">
<p>
Name: <InputText @bind-Value="_editingPerson.Name" />
<ValidationMessage For="@(() => _editingPerson.Name)" />

Blazor: Adding a custom linker config

In your csproj file, add the following:

  <Target Name="AddCustomLinkerDescriptor" AfterTargets="_CollectBlazorLinkerDescriptors">
    <ItemGroup>
      <BlazorLinkerDescriptor Include="CustomLinkerConfig.xml" />
    </ItemGroup>
 
class TaskResultUtil
{
private static ConcurrentDictionary<Type, ITaskResultGetter> _cachedGetters = new ConcurrentDictionary<Type, ITaskResultGetter>();
private interface ITaskResultGetter
{
object GetResult(Task task);
}
private class TaskResultGetter<T> : ITaskResultGetter