質問編集履歴

2

ログイン認証を行うクラスについて記述

2020/04/06 07:35

投稿

opo3939222
opo3939222

スコア6

test CHANGED
File without changes
test CHANGED
@@ -30,61 +30,291 @@
30
30
 
31
31
  ```
32
32
 
33
- `@using Microsoft.AspNetCore.http`
33
+ @using Microsoft.AspNetCore.http
34
-
34
+
35
- `@inject Httpcontext httpcontext`
35
+ @inject Httpcontext httpcontext
36
-
37
-
38
-
36
+
37
+
38
+
39
- `<ul class="navbar-nav">`
39
+ <ul class="navbar-nav">
40
-
40
+
41
- `@if ((httpcontext.User.Identity.IsAuthenticated)`
41
+ @if ((httpcontext.User.Identity.IsAuthenticated)
42
-
42
+
43
- `{`
43
+ {`
44
-
44
+
45
- ` <li class="nav-item">`
45
+ <li class="nav-item">
46
-
46
+
47
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>`
47
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
48
-
48
+
49
- ` </li>`
49
+ </li>
50
-
50
+
51
- ` <li class="nav-item">`
51
+ <li class="nav-item">
52
-
52
+
53
- ` <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post" >
53
+ <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post" >
54
54
 
55
55
  <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
56
56
 
57
57
  </form>
58
58
 
59
- </li>`
59
+ </li>
60
-
60
+
61
- `}`
61
+ }
62
-
62
+
63
- `else`
63
+ else
64
-
64
+
65
- `{`
65
+ {
66
-
66
+
67
- ` <li class="nav-item">`
67
+ <li class="nav-item">
68
-
68
+
69
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>`
69
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
70
-
70
+
71
- ` </li>`
71
+ </li>
72
-
72
+
73
- ` <li class="nav-item">`
73
+ <li class="nav-item">
74
-
74
+
75
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>`
75
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
76
-
76
+
77
- ` </li>`
77
+ </li>
78
-
78
+
79
- `}`
79
+ }
80
-
80
+
81
- `</ul>`
81
+ </ul>
82
82
 
83
83
  ```
84
84
 
85
85
 
86
86
 
87
-
87
+ ```
88
+
89
+ ログインを行うクラス
90
+
91
+
92
+
93
+ using System;
94
+
95
+ using System.Collections.Generic;
96
+
97
+ using System.ComponentModel.DataAnnotations;
98
+
99
+ using System.Linq;
100
+
101
+ using System.Security.Claims;
102
+
103
+ using System.Threading.Tasks;
104
+
105
+ using Microsoft.AspNetCore.Authentication;
106
+
107
+ using Microsoft.AspNetCore.Authentication.Cookies;
108
+
109
+ using Microsoft.AspNetCore.Authorization;
110
+
111
+ using Microsoft.AspNetCore.Mvc;
112
+
113
+ using Microsoft.AspNetCore.Mvc.RazorPages;
114
+
115
+ using Microsoft.EntityFrameworkCore;
116
+
117
+ using WebApp8.Data;
118
+
119
+ using WebApp8.Models;
120
+
121
+
122
+
123
+ namespace WebApp8.Pages.Account
124
+
125
+ {
126
+
127
+ [AllowAnonymous]
128
+
129
+ public class LoginModel : PageModel
130
+
131
+ {
132
+
133
+
134
+
135
+
136
+
137
+
138
+
139
+ private readonly DBsetuzokuContext _context;
140
+
141
+
142
+
143
+ public LoginModel(DBsetuzokuContext context)
144
+
145
+ {
146
+
147
+ _context = context;
148
+
149
+ }
150
+
151
+
152
+
153
+ [BindProperty]
154
+
155
+ public InputModel Input { get; set; }
156
+
157
+
158
+
159
+ public class InputModel
160
+
161
+ {
162
+
163
+ [Required]
164
+
165
+
166
+
167
+ public string LoginID { get; set; }
168
+
169
+
170
+
171
+ [Required]
172
+
173
+ [DataType(DataType.Password)]
174
+
175
+ public string Password { get; set; }
176
+
177
+
178
+
179
+ [Display(Name = "Remember me?")]
180
+
181
+ public bool RememberMe { get; set; }
182
+
183
+
184
+
185
+ }
186
+
187
+
188
+
189
+ [TempData]
190
+
191
+ public string ErrorMessage { get; set; }
192
+
193
+
194
+
195
+
196
+
197
+ public IList<User> User { get; set; }
198
+
199
+
200
+
201
+
202
+
203
+
204
+
205
+ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
206
+
207
+ {
208
+
209
+
210
+
211
+
212
+
213
+ if (!ModelState.IsValid) return Page();
214
+
215
+
216
+
217
+
218
+
219
+ // ==================================================
220
+
221
+ // 認証処理
222
+
223
+
224
+
225
+ var result = from u in _context.User
226
+
227
+ select u;
228
+
229
+ result = result.Where(s => s.UserLoginID == Input.LoginID);
230
+
231
+
232
+
233
+ User = await result.ToListAsync();
234
+
235
+ if (User.Count == 0)
236
+
237
+ {
238
+
239
+ //bool isValid = false;
240
+
241
+ return Page();
242
+
243
+ }
244
+
245
+ if (User[0].Password != Input.Password)
246
+
247
+ {
248
+
249
+ //bool isValid = true;
250
+
251
+ return Page();
252
+
253
+ }
254
+
255
+
256
+
257
+
258
+
259
+ Claim[] claims = {
260
+
261
+ new Claim(ClaimTypes.NameIdentifier, Input.LoginID),
262
+
263
+ new Claim(ClaimTypes.Name, Input.LoginID),
264
+
265
+ };
266
+
267
+
268
+
269
+ // 一意の ID 情報
270
+
271
+ var claimsIdentity = new ClaimsIdentity(
272
+
273
+ claims, CookieAuthenticationDefaults.AuthenticationScheme);
274
+
275
+
276
+
277
+ // ログイン
278
+
279
+ await HttpContext.SignInAsync(
280
+
281
+ CookieAuthenticationDefaults.AuthenticationScheme,
282
+
283
+ new ClaimsPrincipal(claimsIdentity),
284
+
285
+ new AuthenticationProperties
286
+
287
+ {
288
+
289
+ // Cookie をブラウザー セッション間で永続化するか?(ブラウザを閉じてもログアウトしないかどうか)
290
+
291
+ IsPersistent = Input.RememberMe
292
+
293
+
294
+
295
+ });
296
+
297
+
298
+
299
+ return LocalRedirect(returnUrl ?? Url.Content("~/"));
300
+
301
+
302
+
303
+
304
+
305
+
306
+
307
+ }
308
+
309
+ }
310
+
311
+
312
+
313
+ }
314
+
315
+
316
+
317
+ ```
88
318
 
89
319
 
90
320
 

1

表記が崩れていたので修正しました。

2020/04/06 07:35

投稿

opo3939222
opo3939222

スコア6

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,4 @@
1
- コード
2
-
3
- ```### 前提・実現したいこと
1
+ ### 前提・実現したいこと
4
2
 
5
3
  ASP.Net Core 3.1でCookie認証を使ってログインを行うページを現在作っております。
6
4