Skip to content

Commit 1d0761a

Browse files
remove dupicate Identity (#18061)
* remove dupicate Identity * remove dupicate Identity * remove dupicate Identity * remove dupicate Identity * work
1 parent 83111b4 commit 1d0761a

2 files changed

Lines changed: 86 additions & 2 deletions

File tree

aspnetcore/security/authentication/scaffold-identity.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how to scaffold Identity in an ASP.NET Core project.
55
monikerRange: '>= aspnetcore-2.1'
66
ms.author: riande
77
ms.custom: mvc
8-
ms.date: 01/15/2020
8+
ms.date: 5/1/2020
99
uid: security/authentication/scaffold-identity
1010
---
1111
# Scaffold Identity in ASP.NET Core projects
@@ -24,7 +24,19 @@ We recommend using a source control system that shows file differences and allow
2424

2525
Services are required when using [Two Factor Authentication](xref:security/authentication/identity-enable-qrcodes), [Account confirmation and password recovery](xref:security/authentication/accconfirm), and other security features with Identity. Services or service stubs aren't generated when scaffolding Identity. Services to enable these features must be added manually. For example, see [Require Email Confirmation](xref:security/authentication/accconfirm#require-email-confirmation).
2626

27-
This document contains more complete instructions than the *ScaffoldingReadme.txt* file which is generated when running the scaffolder.
27+
When scaffolding Identity with a new data context into a project with existing individual accounts:
28+
29+
* In `Startup.ConfigureServices`, remove the calls to:
30+
* `AddDbContext`
31+
* `AddDefaultIdentity`
32+
33+
For example, `AddDbContext` and `AddDefaultIdentity` are commented out in the following code:
34+
35+
[!code-csharp[](scaffold-identity/3.1sample/StartupRemove.cs?name=snippet)]
36+
37+
The preceeding code comments out the code that is duplicated in *Areas/Identity/IdentityHostingStartup.cs*
38+
39+
Typically, apps that were created with individual accounts should ***not*** create a new data context.
2840

2941
## Scaffold identity into an empty project
3042

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using Microsoft.AspNetCore.Builder;
6+
using Microsoft.AspNetCore.Identity;
7+
using Microsoft.AspNetCore.Identity.UI;
8+
using Microsoft.AspNetCore.Hosting;
9+
using Microsoft.AspNetCore.HttpsPolicy;
10+
using Microsoft.EntityFrameworkCore;
11+
using AddDefaultIdentity.Data;
12+
using Microsoft.Extensions.Configuration;
13+
using Microsoft.Extensions.DependencyInjection;
14+
using Microsoft.Extensions.Hosting;
15+
16+
namespace AddDefaultIdentity
17+
{
18+
public class StartupRemove
19+
{
20+
public StartupRemove(IConfiguration configuration)
21+
{
22+
Configuration = configuration;
23+
}
24+
25+
public IConfiguration Configuration { get; }
26+
27+
// This method gets called by the runtime. Use this method to add services to the container.
28+
#region snippet
29+
public void ConfigureServices(IServiceCollection services)
30+
{
31+
//services.AddDbContext<ApplicationDbContext>(options =>
32+
// options.UseSqlServer(
33+
// Configuration.GetConnectionString("DefaultConnection")));
34+
//services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
35+
// .AddEntityFrameworkStores<ApplicationDbContext>();
36+
services.AddControllersWithViews();
37+
services.AddRazorPages();
38+
}
39+
#endregion
40+
41+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
42+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
43+
{
44+
if (env.IsDevelopment())
45+
{
46+
app.UseDeveloperExceptionPage();
47+
app.UseDatabaseErrorPage();
48+
}
49+
else
50+
{
51+
app.UseExceptionHandler("/Home/Error");
52+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
53+
app.UseHsts();
54+
}
55+
app.UseHttpsRedirection();
56+
app.UseStaticFiles();
57+
58+
app.UseRouting();
59+
60+
app.UseAuthentication();
61+
app.UseAuthorization();
62+
63+
app.UseEndpoints(endpoints =>
64+
{
65+
endpoints.MapControllerRoute(
66+
name: "default",
67+
pattern: "{controller=Home}/{action=Index}/{id?}");
68+
endpoints.MapRazorPages();
69+
});
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)