File tree Expand file tree Collapse file tree 1 file changed +45
-0
lines changed
Expand file tree Collapse file tree 1 file changed +45
-0
lines changed Original file line number Diff line number Diff line change @@ -83,5 +83,50 @@ public void ConfigureServices(IServiceCollection services)
8383 });
8484}
8585```
86+ or using dependency injection:
8687
88+ ``` c#
89+ public class AuthenticationEvents : BasicAuthenticationEvents
90+ {
91+ #region Public Methods
92+
93+ /// <inheritdoc />
94+ public override Task ValidatePrincipalAsync (ValidatePrincipalContext context )
95+ {
96+ if ((context .UserName == " userName" ) && (context .Password == " password" ))
97+ {
98+ var claims = new List <Claim >
99+ {
100+ new Claim (ClaimTypes .Name , context .UserName , context .Options .ClaimsIssuer )
101+ };
102+
103+ var principal = new ClaimsPrincipal (new ClaimsIdentity (claims , BasicAuthenticationDefaults .AuthenticationScheme ));
104+ context .Principal = principal ;
105+ context .AuthenticationFailMessage = null ;
106+ }
107+
108+ return Task .CompletedTask ;
109+ }
110+
111+ #endregion
112+ }
87113
114+ ```
115+
116+ and then registration
117+
118+ ``` c#
119+ public void ConfigureServices (IServiceCollection services )
120+ {
121+ services .AddScoped <AuthenticationEvents >();
122+
123+ services
124+ .AddAuthentication (BasicAuthenticationDefaults .AuthenticationScheme )
125+ .AddBasicAuthentication (
126+ options =>
127+ {
128+ options .Realm = " My Application" ;
129+ options .EventsType = typeof (AuthenticationEvents )
130+ });
131+ }
132+ ```
You can’t perform that action at this time.
0 commit comments