Skip to content

Instantly share code, notes, and snippets.

View thivi's full-sized avatar
🏏

Theviyanthan Krishnamohan thivi

🏏
View GitHub Profile
import React, { useState } from 'react';
const Timer = () => {
const [seconds, setSeconds] = useState(0);
const [isActive, setIsActive] = useState(false);
const handleTimerClick = () => {
setIsActive(!isActive);
};
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Secure">Secure</a>
</li>
@{
ViewData["Title"] = "Secure";
}
<h1>@ViewData["Title"]</h1>
<p>Use this page to show sensitive information that should only be shown to an authenticated user.</p>
<div>
<h4>Profile Pic</h4>
<img src=@Model.ProfileURL class="profile-pic"/>
using System;
namespace asp.net_core_boilerplte.Models
{
public class Secure
{
public IEnumerable<System.Security.Claims.Claim>? Claims { get; set; }
public string? AccessToken { get; set; }
public string? IdToken { get; set; }
public string? RefreshToken { get; set; }
public string? DisplayName { get; set; }
@thivi
thivi / home.cs
Last active April 22, 2023 18:46
[Authorize]
public async Task<IActionResult> Secure()
{
var accessToken = await HttpContext.GetTokenAsync("access_token");
var idToken = await HttpContext.GetTokenAsync("id_token");
var refreshToken = await HttpContext.GetTokenAsync("refresh_token");
string displayName = UserUtils.GetDisplayName(User);
IEnumerable<System.Security.Claims.Claim> claims = User.Claims;
// Getting the profile picture URL from the userinfo endpoint
@thivi
thivi / User.cs
Last active April 22, 2023 18:53
using System;
namespace asp.net_core_boilerplte.Utils
{
public static class UserUtils
{
public static string GetDisplayName(System.Security.Claims.ClaimsPrincipal user)
{
try
{
return user?.Claims?.Where(claim =>
@using asp.net_core_boilerplte.Utils;
<ul class="navbar-nav">
@if (User?.Identity?.IsAuthenticated == true)
{
<li class="nav-item">
<a class="nav-link text-dark" title="Manage">Hello @UserUtils.GetDisplayName(User)!</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Identity" asp-action="Logout">Logout</a>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.IdentityModel.Protocols.OpenIdConnect;
using Microsoft.Extensions.Configuration;
IConfiguration Configuration = new ConfigurationBuilder()
.SetBasePath(AppDomain.CurrentDomain.BaseDirectory)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
app.UseAuthentication();
app.UseAuthorization();