Skip to content

Commit 03c0971

Browse files
committed
update README
1 parent 4341fa6 commit 03c0971

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
```

0 commit comments

Comments
 (0)