質問編集履歴

2

初心者アイコンをつけました

2021/07/08 08:32

投稿

Mk-ky
Mk-ky

スコア19

test CHANGED
File without changes
test CHANGED
@@ -38,95 +38,355 @@
38
38
 
39
39
  using System.Linq;
40
40
 
41
+ using System.Threading.Tasks; 
42
+
43
+ using プロジェクト.Models;
44
+
45
+ using Microsoft.AspNetCore.Authorization;
46
+
47
+
48
+
49
+ namespace プロジェクト.Controllers
50
+
51
+ {
52
+
53
+
54
+
55
+ [Authorize]
56
+
57
+ public class HomeController : Controller
58
+
59
+ {
60
+
61
+ private readonly ILogger<HomeController> _logger;
62
+
63
+
64
+
65
+
66
+
67
+ public HomeController(ILogger<HomeController> logger)
68
+
69
+ {
70
+
71
+ _logger = logger;
72
+
73
+ }
74
+
75
+
76
+
77
+
78
+
79
+
80
+
81
+ public IActionResult Index()
82
+
83
+ {
84
+
85
+ return View();
86
+
87
+ }
88
+
89
+
90
+
91
+ public IActionResult Privacy()
92
+
93
+ {
94
+
95
+ return View();
96
+
97
+ }
98
+
99
+
100
+
101
+
102
+
103
+     //ここから追加
104
+
105
+ public IActionResult Login()
106
+
107
+ {
108
+
109
+ return View("Areas/Identity/Pages/Account/Login.cshtml");
110
+
111
+ }
112
+
113
+     //ここまで追加
114
+
115
+
116
+
117
+
118
+
119
+ [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
120
+
121
+ public IActionResult Error()
122
+
123
+ {
124
+
125
+ return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
126
+
127
+ }
128
+
129
+ }
130
+
131
+ }
132
+
133
+
134
+
135
+ ```
136
+
137
+
138
+
139
+ #####Startup.cs
140
+
141
+ ```C#
142
+
143
+ using Microsoft.AspNetCore.Builder;
144
+
145
+ using Microsoft.AspNetCore.Hosting;
146
+
147
+ using Microsoft.AspNetCore.HttpsPolicy;
148
+
149
+ using Microsoft.AspNetCore.Identity;
150
+
151
+ using Microsoft.AspNetCore.Identity.UI;
152
+
153
+ using Microsoft.EntityFrameworkCore;
154
+
155
+ using Microsoft.Extensions.Configuration;
156
+
157
+ using Microsoft.Extensions.DependencyInjection;
158
+
159
+ using Microsoft.Extensions.Hosting;
160
+
161
+ using System;
162
+
163
+ using System.Collections.Generic;
164
+
165
+ using System.Linq;
166
+
41
167
  using System.Threading.Tasks;
42
168
 
43
- using プロジェクト.Models;
169
+ using プロジェクト.Data;
44
-
45
- using Microsoft.AspNetCore.Authorization;
170
+
46
-
47
-
48
-
171
+
172
+
49
- namespace プロジェクト.Controllers
173
+ namespace プロジェクト
50
174
 
51
175
  {
52
176
 
53
-
54
-
55
- [Authorize]
56
-
57
- public class HomeController : Controller
177
+ public class Startup
58
178
 
59
179
  {
60
180
 
61
- private readonly ILogger<HomeController> _logger;
62
-
63
-
64
-
65
-
66
-
67
- public HomeController(ILogger<HomeController> logger)
68
-
69
- {
70
-
71
- _logger = logger;
72
-
73
- }
74
-
75
-
76
-
77
-
181
+ public Startup(IConfiguration configuration)
182
+
183
+ {
184
+
185
+ Configuration = configuration;
186
+
187
+ }
188
+
189
+
190
+
191
+ public IConfiguration Configuration { get; }
192
+
193
+
194
+
195
+ // This method gets called by the runtime. Use this method to add services to the container.
196
+
197
+ public void ConfigureServices(IServiceCollection services)
198
+
199
+ {
200
+
201
+ services.AddRazorPages();
202
+
203
+
204
+
205
+ services.AddDbContext<ApplicationDbContext>(options =>
206
+
207
+ options.UseSqlServer(
208
+
209
+ Configuration.GetConnectionString("DefaultConnection")));
210
+
211
+ services.AddDatabaseDeveloperPageExceptionFilter();
212
+
213
+
214
+
215
+ services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
216
+
217
+ .AddEntityFrameworkStores<ApplicationDbContext>();
218
+
219
+ services.AddControllersWithViews();
220
+
221
+ }
222
+
223
+
224
+
225
+ // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
226
+
227
+ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
228
+
229
+ {
230
+
231
+ if (env.IsDevelopment())
232
+
233
+ {
234
+
235
+ app.UseDeveloperExceptionPage();
236
+
237
+ app.UseMigrationsEndPoint();
238
+
239
+ }
240
+
241
+ else
242
+
243
+ {
244
+
245
+ app.UseExceptionHandler("/Home/Error");
246
+
247
+ // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
248
+
249
+ app.UseHsts();
250
+
251
+ }
252
+
253
+ app.UseHttpsRedirection();
254
+
255
+ app.UseStaticFiles();
256
+
257
+
258
+
259
+ app.UseRouting();
260
+
261
+
262
+
263
+ app.UseAuthentication();
264
+
265
+ app.UseAuthorization();
266
+
267
+
268
+
269
+ app.UseEndpoints(endpoints =>
270
+
271
+ {
272
+
273
+ endpoints.MapRazorPages();
274
+
275
+ endpoints.MapControllerRoute(
276
+
277
+ name: "default",
278
+
279
+ pattern: "{controller=Home}/{action=Login}/{id?}");
280
+
281
+ });
282
+
283
+ }
284
+
285
+ }
286
+
287
+ }
288
+
289
+
290
+
291
+ ```
292
+
293
+ 以下ログインページのコード
294
+
295
+ #####Login.cshtml
296
+
297
+ ```Razor
298
+
299
+ @page
300
+
301
+
302
+
303
+ @model LoginModel
304
+
305
+
306
+
307
+ @{
308
+
309
+ ViewData["Message3"] = "ログイン";
310
+
311
+ }
312
+
313
+
314
+
315
+ <h1> @ViewData["Message3"] </h1> 
316
+
317
+ <div class="row">
318
+
319
+ <div class="col-md-4">
320
+
321
+ <section>
322
+
323
+ <form id="account" method="post">
324
+
325
+ <h4>IDを入力してログインします</h4>
326
+
327
+ <hr />
328
+
329
+ <div class="form-group">
330
+
331
+ <label asp-for="Input.Email">ID</label>
332
+
333
+ <input asp-for="Input.Email" class="form-control" />
334
+
335
+ <span 9="Input.Email" class="text-danger"></span>
336
+
337
+ </div>
338
+
339
+ <div class="form-group">
340
+
341
+ <label asp-for="Input.Password">パスワード</label>
342
+
343
+ <input asp-for="Input.Password" class="form-control" />
344
+
345
+ <span asp-validation-for="Input.Password" class="text-danger"></span>
346
+
347
+ </div>
348
+
349
+ <div class="form-group">
350
+
351
+ <div class="checkbox">
352
+
353
+ <label asp-for="Input.RememberMe">
354
+
355
+
356
+
357
+ <input asp-for="Input.RememberMe" />
358
+
359
+ @Html.DisplayNameFor(m => m.Input.RememberMe)
360
+
361
+
362
+
363
+ </label>
364
+
365
+ </div>
366
+
367
+ </div>
368
+
369
+ <div class="form-group">
370
+
371
+ <button type="submit" class="btn btn-primary">Log in</button>
372
+
373
+ </div>
78
374
 
79
375
 
80
376
 
377
+ </form>
378
+
81
- public IActionResult Index()
379
+ </section>
82
-
83
- {
380
+
84
-
85
- return View();
86
-
87
- }
88
-
89
-
90
-
91
- public IActionResult Privacy()
92
-
93
- {
94
-
95
- return View();
96
-
97
- }
98
-
99
-
100
-
101
-
102
-
103
-     //ここから追加
381
+ </div>
104
-
105
- public IActionResult Login()
382
+
106
-
107
- {
108
-
109
- return View("Areas/Identity/Pages/Account/Login.cshtml");
110
-
111
- }
112
-
113
-     //ここまで追加
383
+ </div>
384
+
385
+
386
+
114
-
387
+ @section Scripts {
115
-
116
-
117
-
118
-
388
+
119
- [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
389
+ <partial name="_ValidationScriptsPartial" />
120
-
121
- public IActionResult Error()
122
-
123
- {
124
-
125
- return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
126
-
127
- }
128
-
129
- }
130
390
 
131
391
  }
132
392
 
@@ -136,266 +396,6 @@
136
396
 
137
397
 
138
398
 
139
- #####Startup.cs
140
-
141
- ```C#
142
-
143
- using Microsoft.AspNetCore.Builder;
144
-
145
- using Microsoft.AspNetCore.Hosting;
146
-
147
- using Microsoft.AspNetCore.HttpsPolicy;
148
-
149
- using Microsoft.AspNetCore.Identity;
150
-
151
- using Microsoft.AspNetCore.Identity.UI;
152
-
153
- using Microsoft.EntityFrameworkCore;
154
-
155
- using Microsoft.Extensions.Configuration;
156
-
157
- using Microsoft.Extensions.DependencyInjection;
158
-
159
- using Microsoft.Extensions.Hosting;
160
-
161
- using System;
162
-
163
- using System.Collections.Generic;
164
-
165
- using System.Linq;
166
-
167
- using System.Threading.Tasks;
168
-
169
- using プロジェクト.Data;
170
-
171
-
172
-
173
- namespace プロジェクト
174
-
175
- {
176
-
177
- public class Startup
178
-
179
- {
180
-
181
- public Startup(IConfiguration configuration)
182
-
183
- {
184
-
185
- Configuration = configuration;
186
-
187
- }
188
-
189
-
190
-
191
- public IConfiguration Configuration { get; }
192
-
193
-
194
-
195
- // This method gets called by the runtime. Use this method to add services to the container.
196
-
197
- public void ConfigureServices(IServiceCollection services)
198
-
199
- {
200
-
201
- services.AddRazorPages();
202
-
203
-
204
-
205
- services.AddDbContext<ApplicationDbContext>(options =>
206
-
207
- options.UseSqlServer(
208
-
209
- Configuration.GetConnectionString("DefaultConnection")));
210
-
211
- services.AddDatabaseDeveloperPageExceptionFilter();
212
-
213
-
214
-
215
- services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
216
-
217
- .AddEntityFrameworkStores<ApplicationDbContext>();
218
-
219
- services.AddControllersWithViews();
220
-
221
- }
222
-
223
-
224
-
225
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
226
-
227
- public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
228
-
229
- {
230
-
231
- if (env.IsDevelopment())
232
-
233
- {
234
-
235
- app.UseDeveloperExceptionPage();
236
-
237
- app.UseMigrationsEndPoint();
238
-
239
- }
240
-
241
- else
242
-
243
- {
244
-
245
- app.UseExceptionHandler("/Home/Error");
246
-
247
- // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
248
-
249
- app.UseHsts();
250
-
251
- }
252
-
253
- app.UseHttpsRedirection();
254
-
255
- app.UseStaticFiles();
256
-
257
-
258
-
259
- app.UseRouting();
260
-
261
-
262
-
263
- app.UseAuthentication();
264
-
265
- app.UseAuthorization();
266
-
267
-
268
-
269
- app.UseEndpoints(endpoints =>
270
-
271
- {
272
-
273
- endpoints.MapRazorPages();
274
-
275
- endpoints.MapControllerRoute(
276
-
277
- name: "default",
278
-
279
- pattern: "{controller=Home}/{action=Login}/{id?}");
280
-
281
- });
282
-
283
- }
284
-
285
- }
286
-
287
- }
288
-
289
-
290
-
291
- ```
292
-
293
- 以下ログインページのコード
294
-
295
- #####Login.cshtml
296
-
297
- ```Razor
298
-
299
- @page
300
-
301
-
302
-
303
- @model LoginModel
304
-
305
-
306
-
307
- @{
308
-
309
- ViewData["Message3"] = "ログイン";
310
-
311
- }
312
-
313
-
314
-
315
- <h1> @ViewData["Message3"] </h1> 
316
-
317
- <div class="row">
318
-
319
- <div class="col-md-4">
320
-
321
- <section>
322
-
323
- <form id="account" method="post">
324
-
325
- <h4>IDを入力してログインします</h4>
326
-
327
- <hr />
328
-
329
- <div class="form-group">
330
-
331
- <label asp-for="Input.Email">ID</label>
332
-
333
- <input asp-for="Input.Email" class="form-control" />
334
-
335
- <span 9="Input.Email" class="text-danger"></span>
336
-
337
- </div>
338
-
339
- <div class="form-group">
340
-
341
- <label asp-for="Input.Password">パスワード</label>
342
-
343
- <input asp-for="Input.Password" class="form-control" />
344
-
345
- <span asp-validation-for="Input.Password" class="text-danger"></span>
346
-
347
- </div>
348
-
349
- <div class="form-group">
350
-
351
- <div class="checkbox">
352
-
353
- <label asp-for="Input.RememberMe">
354
-
355
-
356
-
357
- <input asp-for="Input.RememberMe" />
358
-
359
- @Html.DisplayNameFor(m => m.Input.RememberMe)
360
-
361
-
362
-
363
- </label>
364
-
365
- </div>
366
-
367
- </div>
368
-
369
- <div class="form-group">
370
-
371
- <button type="submit" class="btn btn-primary">Log in</button>
372
-
373
- </div>
374
-
375
-
376
-
377
- </form>
378
-
379
- </section>
380
-
381
- </div>
382
-
383
- </div>
384
-
385
-
386
-
387
- @section Scripts {
388
-
389
- <partial name="_ValidationScriptsPartial" />
390
-
391
- }
392
-
393
-
394
-
395
- ```
396
-
397
-
398
-
399
399
 
400
400
 
401
401
 

1

startup.csでLoginメソッドではなくIndexメソッドがアクションに設定されていたので修正しました 正しくはLoginメソッドです。

2021/07/08 08:32

投稿

Mk-ky
Mk-ky

スコア19

test CHANGED
File without changes
test CHANGED
@@ -276,7 +276,7 @@
276
276
 
277
277
  name: "default",
278
278
 
279
- pattern: "{controller=Home}/{action=Index}/{id?}");
279
+ pattern: "{controller=Home}/{action=Login}/{id?}");
280
280
 
281
281
  });
282
282