Ver código fonte

authroization guard setup

tags/v1.1.0^2
anastasijasavov 3 anos atrás
pai
commit
821e0ceaf4

+ 3
- 2
GrpcShared/Interfaces/IStatsService.cs Ver arquivo

@@ -1,4 +1,5 @@
using ProtoBuf.Grpc.Configuration;
using GrpcShared.DTO.Track;
using ProtoBuf.Grpc.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -10,6 +11,6 @@ namespace GrpcShared.Interfaces
[Service]
public interface IStatsService
{
Task<Track>
Task<TrackResponse> GetCurrentlyPlayingTrack(string token);
}
}

+ 7
- 3
NemAnCore/App.razor Ver arquivo

@@ -1,6 +1,9 @@
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
<CascadingAuthenticationState>
<Router AppAssembly="@typeof(App).Assembly">
<Found Context="routeData">
<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" >
<NotAuthorized>Sorry nisi autorizovan</NotAuthorized>
</AuthorizeRouteView>
<FocusOnNavigate RouteData="@routeData" Selector="h1" />
</Found>
<NotFound>
@@ -10,3 +13,4 @@
</LayoutView>
</NotFound>
</Router>
</CascadingAuthenticationState>

+ 26
- 0
NemAnCore/AuthProvider.cs Ver arquivo

@@ -0,0 +1,26 @@
using Blazored.SessionStorage;
using System.Security.Claims;

namespace NemAnBlazor
{
public class AuthProvider : AuthenticationStateProvider
{
private readonly ISessionStorageService _sessionStorage;
public AuthProvider(ISessionStorageService sessionStorage)
{
_sessionStorage = sessionStorage;
}
public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
string token = await _sessionStorage.GetItemAsync<string>("token");

ClaimsIdentity identity = new ();
ClaimsPrincipal user = new (identity);
AuthenticationState state = new(user);

NotifyAuthenticationStateChanged(Task.FromResult(state));

return state;
}
}
}

+ 1
- 0
NemAnCore/NemAnBlazor.csproj Ver arquivo

@@ -10,6 +10,7 @@
<PackageReference Include="Blazored.SessionStorage" Version="2.2.0" />
<PackageReference Include="Grpc.Net.Client" Version="2.47.0" />
<PackageReference Include="Grpc.Net.Client.Web" Version="2.47.0" />
<PackageReference Include="Microsoft.AspNetCore.Components.Authorization" Version="6.0.8" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.7" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.7" PrivateAssets="all" />
<PackageReference Include="protobuf-net.Grpc" Version="1.0.171" />

+ 4
- 9
NemAnCore/Pages/Callback.razor Ver arquivo

@@ -3,20 +3,15 @@
@inject NavigationManager NavigationMgr
@inject IAuthClientService AuthService
@inject Blazored.SessionStorage.ISessionStorageService sessionStorage
<PageTitle>Callback page</PageTitle>
<PageTitle>Redirecting...</PageTitle>


<p role="status">Current count: @currentCount</p>
<p role="status">Loading...</p>

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

@code {
private int currentCount = 0;

private void IncrementCount()
{
currentCount++;
}
protected override async Task OnInitializedAsync()
{
string url = NavigationMgr.Uri;

+ 9
- 1
NemAnCore/Pages/Home.razor Ver arquivo

@@ -1,7 +1,15 @@
@page "/home"
<h3>Home</h3>

<p>pozdrav, login radi</p>
<p>login radi</p>
<AuthorizeView>
<Authorized>
<p>autorizovan si</p>
</Authorized>
<NotAuthorized>
<p>nisi autorizovan</p>
</NotAuthorized>
</AuthorizeView>
@code {

}

+ 3
- 1
NemAnCore/Program.cs Ver arquivo

@@ -1,3 +1,4 @@
global using Microsoft.AspNetCore.Components.Authorization;
using Blazored.SessionStorage;
using Grpc.Net.Client;
using Grpc.Net.Client.Web;
@@ -25,6 +26,7 @@ builder.Services.AddScoped(_ =>
builder.Services.AddScoped<ITrackClientService, TrackClientService>();
builder.Services.AddScoped<IAuthClientService, AuthClientService>();
builder.Services.AddBlazoredSessionStorage();

builder.Services.AddScoped<AuthenticationStateProvider, AuthProvider>();
builder.Services.AddAuthorizationCore();
await builder.Build().RunAsync();


+ 2
- 1
NemAnCore/_Imports.razor Ver arquivo

@@ -8,4 +8,5 @@
@using Microsoft.JSInterop
@using NemAnBlazor
@using NemAnBlazor.Shared
@using System.Web
@using System.Web
@using Microsoft.AspNetCore.Components.Authorization

+ 11
- 0
gRPCServer/Services/StatsService.cs Ver arquivo

@@ -1,9 +1,20 @@

using GrpcShared.DTO.Track;
using GrpcShared.Interfaces;

namespace SpotifyService.Services
{
public class StatsService : IStatsService
{
private readonly IHttpClientFactory _httpClientFactory;

public StatsService(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
public Task<TrackResponse> GetCurrentlyPlayingTrack(string token)
{
return null;
}
}
}

Carregando…
Cancelar
Salvar