本文整理汇总了C#中IApplicationBuilder.UseBasicAuthentication方法的典型用法代码示例。如果您正苦于以下问题:C# IApplicationBuilder.UseBasicAuthentication方法的具体用法?C# IApplicationBuilder.UseBasicAuthentication怎么用?C# IApplicationBuilder.UseBasicAuthentication使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IApplicationBuilder
的用法示例。
在下文中一共展示了IApplicationBuilder.UseBasicAuthentication方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Configure
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
LifetimeManager lifetimeManager,
InterpreterManager interpreterManager,
SecurityManager securityManager
) {
lifetimeManager.Initialize();
interpreterManager.Initialize();
app.UseWebSockets(new WebSocketOptions {
ReplaceFeature = true,
KeepAliveInterval = TimeSpan.FromMilliseconds(1000000000),
ReceiveBufferSize = 0x10000
});
var routeBuilder = new RouteBuilder(app, new RouteHandler(RemoteUriHelper.HandlerAsync));
routeBuilder.MapRoute("help_and_shiny", "remoteuri");
app.UseRouter(routeBuilder.Build());
app.UseBasicAuthentication(options => {
options.Events = new BasicEvents { OnSignIn = securityManager.SignInAsync };
});
app.Use((context, next) => {
if (!context.User.Identity.IsAuthenticated) {
return context.Authentication.ChallengeAsync();
} else {
return next();
}
});
app.UseMvc();
}
示例2: Configure
public void Configure(IApplicationBuilder app)
{
app.UseBasicAuthentication();
app.Run(async context =>
{
context.Response.ContentType = "text/plain";
await context.Response.WriteAsync("Hello ASP.NET 5!");
});
}
示例3: Run_Simple
public void Run_Simple(IApplicationBuilder app)
{
// this example shows how to configure basic authentication using IOptions
app.UseBasicAuthentication();
app.Run(async (context) =>
{
if (!context.User.Identity.IsAuthenticated)
await context.Authentication.ChallengeAsync();
else
await context.Response.WriteAsync($"Hello {context.User.Identity.Name}! (simple)");
});
}
示例4: Configure
public void Configure(IApplicationBuilder app)
{
app.UseBasicAuthentication(options =>
{
options.Realm = Configuration.Get("BasicAuthentication:Realm");
options.AutomaticAuthentication = true;
options.Notifications = new BasicAuthenticationNotifications
{
CredentialReceived = n =>
{
if (n.Credential.Id == n.Credential.Secret)
{
var id = new ClaimsIdentity("Basic");
id.AddClaim(new Claim(ClaimTypes.Name, n.Credential.Id));
n.AuthenticationTicket = new AuthenticationTicket(
new ClaimsPrincipal(id),
new AuthenticationProperties(),
n.AuthenticationTicket.AuthenticationScheme);
}
return Task.FromResult(0);
}
};
});
app.Run(async (context) =>
{
if (context.User.Identity.IsAuthenticated)
{
await context.Response.WriteAsync($"<h1>Hello {context.User.Identity.Name}!</h1>");
}
else
{
if (context.Request.Path.Value.EndsWith("/protected"))
{
context.Response.StatusCode = 401;
return;
}
await context.Response.WriteAsync("<h1>anonymous</h1>");
}
});
}
示例5: Configure
public void Configure(IApplicationBuilder app)
{
app.UseBasicAuthentication(options =>
{
options.Realm = "Heroku";
options.Authenticator = new GenericAuthenticator(
(username, password) => {
if (username == Configuration.Get("ADDON_USERNAME")
&& password == Configuration.Get("ADDON_PASSWORD"))
{
return new ClaimsIdentity("Basic");
}
return null;
}
);
}
);
app.UseMvc();
}
示例6: Run_CustomAuthenticationLogic
public void Run_CustomAuthenticationLogic(IApplicationBuilder app)
{
// this example shows how to use custom authentication logic
app.UseBasicAuthentication(options =>
{
options.Realm = "Custom authentication logic";
options.Events = new BasicEvents()
{
OnSignIn = context =>
{
// instead of hardcoded logic, you could also obtain your services that handle authentication
// from the container by using `app.ApplicationServices.GetService` and use those
if (context.Username == "admin" && context.Password == "1234")
{
var claims = new[]
{
new Claim(ClaimTypes.Name, "administrator")
};
// note that ClaimsIdentity is considered "authenticated" only if it has an "authenticationType"
// returning an unauthenticated principal will in this case result in 403 Forbidden
// returning null will act in this case as if there were no credentials submitted and user will be asked again
context.AuthenticationTicket = new AuthenticationTicket(
new ClaimsPrincipal(new ClaimsIdentity(claims, context.Options.AuthenticationScheme)),
new AuthenticationProperties(),
context.Options.AuthenticationScheme
);
// mark response as handled
// AuthenticationTicket != null -> success
// AuthenticationTicket == null -> fail
context.HandleResponse();
}
return Task.FromResult(0);
}
};
});
app.Run(async (context) =>
{
if (!context.User.Identity.IsAuthenticated)
await context.Authentication.ChallengeAsync();
else
await context.Response.WriteAsync($"Hello {context.User.Identity.Name}! (complex)");
});
}
示例7: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, SampleDataInitializer sampleData, UserManager<ApplicationUser> userManager)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// For more details on creating database during deployment see http://go.microsoft.com/fwlink/?LinkID=615859
try
{
using (var serviceScope = app.ApplicationServices.GetRequiredService<IServiceScopeFactory>()
.CreateScope())
{
serviceScope.ServiceProvider.GetService<ApplicationDbContext>()
.Database.Migrate();
}
}
catch { }
}
app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
app.UseStaticFiles();
app.UseIdentity();
// Refactor this into a seperate class
// Remove hard coding of the password in the installDevices routine!
app.UseBasicAuthentication(o =>
{
o.Realm = $"j64 Alarm";
o.Events = new BasicAuthenticationEvents
{
OnSignIn = c =>
{
var x = userManager.FindByNameAsync(c.UserName);
x.Wait();
if (x.Result != null)
{
var y = userManager.CheckPasswordAsync(x.Result, c.Password);
y.Wait();
if (y.Result == true)
{
var z = userManager.GetClaimsAsync(x.Result);
z.Wait();
var identity = new ClaimsIdentity(z.Result, c.Options.AuthenticationScheme);
c.Principal = new ClaimsPrincipal(identity);
}
}
return Task.FromResult(true);
}
};
});
// To configure external authentication please see http://go.microsoft.com/fwlink/?LinkID=532715
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// Seed some default entries into the database
var task = sampleData.CreateMasterUser();
}
示例8: Configure
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, UserManager<ApplicationUser> userManager, ApplicationDbContext ctx)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseIdentity();
// Add external authentication middleware below. To configure them please see http://go.microsoft.com/fwlink/?LinkID=532715
// Refactor this into a seperate class
// Remove hard coding of the password in the installDevices routine!
app.UseBasicAuthentication(o =>
{
o.Realm = $"j64 Alarm";
o.Events = new BasicAuthenticationEvents
{
OnSignIn = c =>
{
var x = userManager.FindByNameAsync(c.UserName);
x.Wait();
if (x.Result != null)
{
var y = userManager.CheckPasswordAsync(x.Result, c.Password);
y.Wait();
if (y.Result == true)
{
var z = userManager.GetClaimsAsync(x.Result);
z.Wait();
var identity = new ClaimsIdentity(z.Result, c.Options.AuthenticationScheme);
c.Principal = new ClaimsPrincipal(identity);
}
}
return Task.FromResult(true);
}
};
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
// Seed some default entries into the database
var task = new Data.UserDataInitializer(ctx, userManager).CreateMasterUser();
}