问题:
blazor 如何检测授权/未授权
Answer:
这是其中之一ClaimsIdentity 的构造函数 https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity.-ctor?view=netframework-4.8#System_Security_Claims_ClaimsIdentity__ctor_System_Collections_Generic_IEnumerable_System_Security_Claims_Claim__System_String_:
public ClaimsIdentity (
System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims,
string authenticationType);
To 设置为已验证 https://learn.microsoft.com/en-us/dotnet/api/system.security.claims.claimsidentity.isauthenticated?view=netframework-4.8,只需发送一个值到authenticationType
,引用文档:
已验证注意:访问时,将根据 AuthenticationType 属性的值返回 IsAuthenticated 属性的值。
要求IsAuthenticated
.
从以下位置查看这段代码:
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, "mrfibuli"),
}, "Fake authentication type");
对于前面的例子,IsAuthenticated
将会是真的因为ClaimsIdentity
构造函数有"Fake authentication type"
for authenticationType
范围。
总结
如果你创造了你的身份包括 authenticationType
参数表示用户已通过身份验证。如果你创造了你的身份without authenticationType
参数,用户未经过身份验证。
var userService = RequestMyUserService(user, password);
var identity = userService.IsValidUser
? new ClaimsIdentity(
new[] {new Claim(ClaimTypes.Name, "mrfibuli"),},
"My Custom User Service") // authenticated
: new ClaimsIdentity(); // not authenticated
...
更多信息请访问基于声明的身份验证 https://andrewlock.net/introduction-to-authentication-with-asp-net-core/#claims-based-authenticationASP.NET Core 身份验证简介。