Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
rabberbock committed Jun 30, 2019
0 parents commit e8dd7fd
Show file tree
Hide file tree
Showing 159 changed files with 14,350 additions and 0 deletions.
7 changes: 7 additions & 0 deletions App.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<CascadingAuthenticationState>
<Router AppAssembly="typeof(Program).Assembly">
<NotFoundContent>
<p>Sorry, there's nothing at this address.</p>
</NotFoundContent>
</Router>
</CascadingAuthenticationState>
15 changes: 15 additions & 0 deletions BlazorAuth.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<LangVersion>7.3</LangVersion>
<RazorLangVersion>3.0</RazorLangVersion>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Blazor" Version="3.0.0-preview6.19307.2" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.Build" Version="3.0.0-preview6.19307.2" PrivateAssets="all" />
<PackageReference Include="Microsoft.AspNetCore.Blazor.DevServer" Version="3.0.0-preview6.19307.2" PrivateAssets="all" />
</ItemGroup>

</Project>
16 changes: 16 additions & 0 deletions Pages/Counter.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
@page "/counter"

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="@IncrementCount">Click me</button>

@code {
int currentCount = 0;

void IncrementCount()
{
currentCount++;
}
}
55 changes: 55 additions & 0 deletions Pages/FetchData.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@page "/fetchdata"
@inject HttpClient Http

<h1>Weather forecast</h1>

<p>This component demonstrates fetching data from the server.</p>

@if (forecasts == null)
{
<p><em>Loading...</em></p>
}
else
{
<table class="table">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
@foreach (var forecast in forecasts)
{
<tr>
<td>@forecast.Date.ToShortDateString()</td>
<td>@forecast.TemperatureC</td>
<td>@forecast.TemperatureF</td>
<td>@forecast.Summary</td>
</tr>
}
</tbody>
</table>
}

@code {
WeatherForecast[] forecasts;

protected override async Task OnInitAsync()
{
forecasts = await Http.GetJsonAsync<WeatherForecast[]>("sample-data/weather.json");
}

class WeatherForecast
{
public DateTime Date { get; set; }

public int TemperatureC { get; set; }

public int TemperatureF { get; set; }

public string Summary { get; set; }
}
}
39 changes: 39 additions & 0 deletions Pages/Index.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
@page "/"
@using Services.Auth
@inject CustomAuthStateProvider CustomAuthStateProvider
<h1>Hello, world!</h1>

Welcome to your new app.

<SurveyPrompt Title="How is Blazor working for you?" />

<Microsoft.AspNetCore.Components.AuthorizeView>
<NotAuthorized>
Not Authorized
</NotAuthorized>
<Authorizing>
Authorizing
</Authorizing>
<Authorized>
Authorized
@context.User.Identity.Name
</Authorized>
</Microsoft.AspNetCore.Components.AuthorizeView>
<button onclick="@(() => UpdateAuthentication(true))">
Set Auth True
</button>
<button onclick="@(() => UpdateAuthentication(false))">
Set Auth False
</button>

@code
{
[CascadingParameter]
private Task<AuthenticationState> AuthenticationStateTask { get; set; }

private async Task UpdateAuthentication(bool isAuthenticated)
{
CustomAuthStateProvider.Authenticated = isAuthenticated;
CustomAuthStateProvider.NotifyStateChange();
}
}
1 change: 1 addition & 0 deletions Pages/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@layout MainLayout
16 changes: 16 additions & 0 deletions Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.AspNetCore.Blazor.Hosting;

namespace BlazorAuth
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}

public static IWebAssemblyHostBuilder CreateHostBuilder(string[] args) =>
BlazorWebAssemblyHost.CreateDefaultBuilder()
.UseBlazorStartup<Startup>();
}
}
30 changes: 30 additions & 0 deletions Services/Auth/CustomAuthStateProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components;

namespace BlazorAuth.Services.Auth
{
public class CustomAuthStateProvider : AuthenticationStateProvider
{
public static bool Authenticated { get; set ;} = true;
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
var claimsIdentity = Authenticated ? new ClaimsIdentity(new List<Claim>
{
new Claim(ClaimTypes.Name, "test_user")
}, "server_auth")
:
new ClaimsIdentity();
return new AuthenticationState(new ClaimsPrincipal(claimsIdentity));
}

public async Task NotifyStateChange()
{
Console.WriteLine($"Calling NotifyStateChange... with Auth={Authenticated}");
NotifyAuthenticationStateChanged(GetAuthenticationStateAsync());
}

}
}
Loading

0 comments on commit e8dd7fd

Please sign in to comment.