-
Notifications
You must be signed in to change notification settings - Fork 126
Expand file tree
/
Copy path_Host.cshtml
More file actions
42 lines (35 loc) · 1.47 KB
/
_Host.cshtml
File metadata and controls
42 lines (35 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
@page "/"
@namespace BlazorServerApp.Pages
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@using System.Security.Claims;
@using Microsoft.AspNetCore.Authentication;
@using Microsoft.AspNetCore.Authentication.Cookies;
@{
Layout = "_Layout";
int randomNum = Random.Shared.Next();
//
// It has 2/3 chance to be logged in.
if (randomNum % 3 != 0)
{
string username = randomNum.ToString();
var claims = new List<Claim>
{
new Claim(ClaimTypes.Name, username)
};
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignInAsync(
CookieAuthenticationDefaults.AuthenticationScheme,
new ClaimsPrincipal(claimsIdentity),
new AuthenticationProperties());
}
else
{
await HttpContext.SignOutAsync(
CookieAuthenticationDefaults.AuthenticationScheme);
}
//
// The user agent context will be set to the parameter UserAgent in App.razor.
// This is the recommended way to work with HttpContext in Blazor server apps. For more details, please refer to: https://learn.microsoft.com/en-us/aspnet/core/blazor/security/server/interactive-server-side-rendering?view=aspnetcore-7.0#ihttpcontextaccessorhttpcontext-in-razor-components
string userAgent = HttpContext.Request.Headers["User-Agent"];
}
<component param-UserAgent="userAgent" type="typeof(App)" render-mode="Server" />