You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Samples are available under [samples directory](samples).
22
+
21
23
Setting it up is quite simple. You will need basic working knowledge of ASP.NET Core 2.2 or newer to get started using this code.
22
24
23
25
On [**Startup.cs**](#startupcs), as shown below, add 2 lines in *ConfigureServices* method `services.AddAuthentication(BasicDefaults.AuthenticationScheme).AddBasic<BasicUserValidationService>(options => { options.Realm = "My App"; });`. And a line `app.UseAuthentication();` in *Configure* method.
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
41
+
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
45
+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
46
+
//.AddBasic(options => { options.Realm = "My App"; });
47
+
48
+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
49
+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
90
+
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
94
+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
95
+
//.AddBasic(options => { options.Realm = "My App"; });
96
+
97
+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
98
+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
.AddBasic<BasicUserValidationService>(options =>{options.Realm="Sample Web API";});
33
+
// Add the Basic scheme authentication here..
34
+
// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
35
+
// If an implementation of IBasicUserValidationService interface is registered in the dependency register as well as OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of an implementation of IBasicUserValidationService.
// The below AddBasic without type parameter will require OnValidateCredentials delegete on options.Events to be set unless an implementation of IBasicUserValidationService interface is registered in the dependency register.
39
+
// Please note if both the delgate and validation server are set then the delegate will be used instead of BasicUserValidationService.
40
+
//.AddBasic(options =>
41
+
42
+
// The below AddBasic with type parameter will add the BasicUserValidationService to the dependency register.
43
+
// Please note if OnValidateCredentials delegete on options.Events is also set then this delegate will be used instead of BasicUserValidationService.
44
+
.AddBasic<BasicUserValidationService>(options =>
45
+
{
46
+
options.Realm="Sample Web API";
47
+
48
+
//// Optional option to suppress the browser login dialog for ajax calls.
49
+
//options.SuppressWWWAuthenticateHeader = true;
50
+
51
+
//// Optional events to override the basic original logic with custom logic.
52
+
//// Only use this if you know what you are doing at your own risk. Any of the events can be assigned.
53
+
options.Events=newBasicEvents
54
+
{
55
+
56
+
//// A delegate assigned to this property will be invoked just before validating credentials.
57
+
//OnValidateCredentials = async (context) =>
58
+
//{
59
+
// // custom code to handle credentials, create principal and call Success method on context.
60
+
// var userRepository = context.HttpContext.RequestServices.GetRequiredService<IUserRepository>();
61
+
// var user = await userRepository.GetUserByUsername(context.Username);
62
+
// var isValid = user != null && user.Password == context.Password;
// context.Handled(); // important! do not forget to call this method at the end.
122
+
//},
123
+
124
+
//// A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateCredentials delegate is assigned.
125
+
//// It can be used for adding claims, headers, etc to the response.
126
+
//OnAuthenticationSucceeded = (context) =>
127
+
//{
128
+
// //custom code to add extra bits to the success response.
0 commit comments