Просмотр исходного кода

authroization guard setup

tags/v1.1.0^2
anastasijasavov 3 лет назад
Родитель
Сommit
af2fffaec2

+ 3
- 2
GrpcShared/Interfaces/IStatsService.cs Просмотреть файл

using ProtoBuf.Grpc.Configuration;
using GrpcShared.DTO.Track;
using ProtoBuf.Grpc.Configuration;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
[Service] [Service]
public interface IStatsService public interface IStatsService
{ {
Task<Track>
Task<TrackResponse> GetCurrentlyPlayingTrack(string token);
} }
} }

+ 26
- 0
NemAnCore/AuthProvider.cs Просмотреть файл

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;
}
}
}

+ 4
- 9
NemAnCore/Pages/Callback.razor Просмотреть файл

@inject NavigationManager NavigationMgr @inject NavigationManager NavigationMgr
@inject IAuthClientService AuthService @inject IAuthClientService AuthService
@inject Blazored.SessionStorage.ISessionStorageService sessionStorage @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 { @code {
private int currentCount = 0;

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

+ 9
- 1
NemAnCore/Pages/Home.razor Просмотреть файл

@page "/home" @page "/home"
<h3>Home</h3> <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 { @code {


} }

+ 3
- 1
NemAnCore/Program.cs Просмотреть файл

global using Microsoft.AspNetCore.Components.Authorization;
using Blazored.SessionStorage; using Blazored.SessionStorage;
using Grpc.Net.Client; using Grpc.Net.Client;
using Grpc.Net.Client.Web; using Grpc.Net.Client.Web;
builder.Services.AddScoped<ITrackClientService, TrackClientService>(); builder.Services.AddScoped<ITrackClientService, TrackClientService>();
builder.Services.AddScoped<IAuthClientService, AuthClientService>(); builder.Services.AddScoped<IAuthClientService, AuthClientService>();
builder.Services.AddBlazoredSessionStorage(); builder.Services.AddBlazoredSessionStorage();

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



+ 11
- 0
gRPCServer/Services/StatsService.cs Просмотреть файл

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


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

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

Загрузка…
Отмена
Сохранить