forked from dotnet/AspNetCore.Docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Introduction to Identity with ASP.NET Core 2 API's (dotnet#4236)
* Changes to split out the ASP.NET Core 2 Identity cofig from ASP.NET Core 1. * Fix problem with number rendering Show differences between ASP.NET Core 1.x and 2.x config. * Fix extra spaces Add snippet_ prefix to regions * Correct what UseAuthentication does. * Update snippet regions with snippet_ prefix.
- Loading branch information
1 parent
7d0b33a
commit c5649dc
Showing
4 changed files
with
139 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
aspnetcore/security/authentication/identity/sample/src/ASPNETv2-IdentityDemo/Startup.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Identity; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.Configuration; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using WebApplication5.Data; | ||
using WebApplication5.Models; | ||
using WebApplication5.Services; | ||
|
||
namespace WebApplication5 | ||
{ | ||
public class Startup | ||
{ | ||
public Startup(IConfiguration configuration) | ||
{ | ||
Configuration = configuration; | ||
} | ||
|
||
public IConfiguration Configuration { get; } | ||
|
||
#region snippet_configureservices | ||
// This method gets called by the runtime. Use this method to add services to the container. | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddDbContext<ApplicationDbContext>(options => | ||
options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"))); | ||
|
||
services.AddIdentity<ApplicationUser, IdentityRole>() | ||
.AddEntityFrameworkStores<ApplicationDbContext>() | ||
.AddDefaultTokenProviders(); | ||
|
||
services.Configure<IdentityOptions>(options => | ||
{ | ||
// Password settings | ||
options.Password.RequireDigit = true; | ||
options.Password.RequiredLength = 8; | ||
options.Password.RequireNonAlphanumeric = false; | ||
options.Password.RequireUppercase = true; | ||
options.Password.RequireLowercase = false; | ||
options.Password.RequiredUniqueChars = 6; | ||
|
||
// Lockout settings | ||
options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); | ||
options.Lockout.MaxFailedAccessAttempts = 10; | ||
options.Lockout.AllowedForNewUsers = true; | ||
|
||
// User settings | ||
options.User.RequireUniqueEmail = true; | ||
}); | ||
|
||
services.ConfigureApplicationCookie(options => | ||
{ | ||
// Cookie settings | ||
options.Cookie.HttpOnly = true; | ||
options.Cookie.Expiration = TimeSpan.FromDays(150); | ||
options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login | ||
options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout | ||
options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied | ||
options.SlidingExpiration = true; | ||
}); | ||
|
||
// Add application services. | ||
services.AddTransient<IEmailSender, EmailSender>(); | ||
|
||
services.AddMvc(); | ||
} | ||
#endregion | ||
|
||
#region snippet_configure | ||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. | ||
public void Configure(IApplicationBuilder app, IHostingEnvironment env) | ||
{ | ||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
app.UseBrowserLink(); | ||
app.UseDatabaseErrorPage(); | ||
} | ||
else | ||
{ | ||
app.UseExceptionHandler("/Home/Error"); | ||
} | ||
|
||
app.UseStaticFiles(); | ||
|
||
app.UseAuthentication(); | ||
|
||
app.UseMvc(routes => | ||
{ | ||
routes.MapRoute( | ||
name: "default", | ||
template: "{controller=Home}/{action=Index}/{id?}"); | ||
}); | ||
} | ||
#endregion | ||
} | ||
} |