質問編集履歴

2

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

2020/04/06 07:35

投稿

opo3939222
opo3939222

スコア6

title CHANGED
File without changes
body CHANGED
@@ -14,35 +14,150 @@
14
14
 
15
15
 
16
16
  ```
17
- `@using Microsoft.AspNetCore.http`
17
+ @using Microsoft.AspNetCore.http
18
- `@inject Httpcontext httpcontext`
18
+ @inject Httpcontext httpcontext
19
19
 
20
- `<ul class="navbar-nav">`
20
+ <ul class="navbar-nav">
21
- `@if ((httpcontext.User.Identity.IsAuthenticated)`
21
+ @if ((httpcontext.User.Identity.IsAuthenticated)
22
- `{`
22
+ {`
23
- ` <li class="nav-item">`
23
+ <li class="nav-item">
24
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>`
24
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
25
- ` </li>`
25
+ </li>
26
- ` <li class="nav-item">`
26
+ <li class="nav-item">
27
- ` <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post" >
27
+ <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post" >
28
28
  <button type="submit" class="nav-link btn btn-link text-dark">Logout</button>
29
29
  </form>
30
- </li>`
30
+ </li>
31
- `}`
31
+ }
32
- `else`
32
+ else
33
- `{`
33
+ {
34
- ` <li class="nav-item">`
34
+ <li class="nav-item">
35
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>`
35
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Register">Register</a>
36
- ` </li>`
36
+ </li>
37
- ` <li class="nav-item">`
37
+ <li class="nav-item">
38
- ` <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>`
38
+ <a class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Login">Login</a>
39
- ` </li>`
39
+ </li>
40
- `}`
40
+ }
41
- `</ul>`
41
+ </ul>
42
42
  ```
43
43
 
44
+ ```
45
+ ログインを行うクラス
44
46
 
47
+ using System;
48
+ using System.Collections.Generic;
49
+ using System.ComponentModel.DataAnnotations;
50
+ using System.Linq;
51
+ using System.Security.Claims;
52
+ using System.Threading.Tasks;
53
+ using Microsoft.AspNetCore.Authentication;
54
+ using Microsoft.AspNetCore.Authentication.Cookies;
55
+ using Microsoft.AspNetCore.Authorization;
56
+ using Microsoft.AspNetCore.Mvc;
57
+ using Microsoft.AspNetCore.Mvc.RazorPages;
58
+ using Microsoft.EntityFrameworkCore;
59
+ using WebApp8.Data;
60
+ using WebApp8.Models;
45
61
 
62
+ namespace WebApp8.Pages.Account
63
+ {
64
+ [AllowAnonymous]
65
+ public class LoginModel : PageModel
66
+ {
67
+
68
+
69
+
70
+ private readonly DBsetuzokuContext _context;
71
+
72
+ public LoginModel(DBsetuzokuContext context)
73
+ {
74
+ _context = context;
75
+ }
76
+
77
+ [BindProperty]
78
+ public InputModel Input { get; set; }
79
+
80
+ public class InputModel
81
+ {
82
+ [Required]
83
+
84
+ public string LoginID { get; set; }
85
+
86
+ [Required]
87
+ [DataType(DataType.Password)]
88
+ public string Password { get; set; }
89
+
90
+ [Display(Name = "Remember me?")]
91
+ public bool RememberMe { get; set; }
92
+
93
+ }
94
+
95
+ [TempData]
96
+ public string ErrorMessage { get; set; }
97
+
98
+
99
+ public IList<User> User { get; set; }
100
+
101
+
102
+
103
+ public async Task<IActionResult> OnPostAsync(string returnUrl = null)
104
+ {
105
+
106
+
107
+ if (!ModelState.IsValid) return Page();
108
+
109
+
110
+ // ==================================================
111
+ // 認証処理
112
+
113
+ var result = from u in _context.User
114
+ select u;
115
+ result = result.Where(s => s.UserLoginID == Input.LoginID);
116
+
117
+ User = await result.ToListAsync();
118
+ if (User.Count == 0)
119
+ {
120
+ //bool isValid = false;
121
+ return Page();
122
+ }
123
+ if (User[0].Password != Input.Password)
124
+ {
125
+ //bool isValid = true;
126
+ return Page();
127
+ }
128
+
129
+
130
+ Claim[] claims = {
131
+ new Claim(ClaimTypes.NameIdentifier, Input.LoginID),
132
+ new Claim(ClaimTypes.Name, Input.LoginID),
133
+ };
134
+
135
+ // 一意の ID 情報
136
+ var claimsIdentity = new ClaimsIdentity(
137
+ claims, CookieAuthenticationDefaults.AuthenticationScheme);
138
+
139
+ // ログイン
140
+ await HttpContext.SignInAsync(
141
+ CookieAuthenticationDefaults.AuthenticationScheme,
142
+ new ClaimsPrincipal(claimsIdentity),
143
+ new AuthenticationProperties
144
+ {
145
+ // Cookie をブラウザー セッション間で永続化するか?(ブラウザを閉じてもログアウトしないかどうか)
146
+ IsPersistent = Input.RememberMe
147
+
148
+ });
149
+
150
+ return LocalRedirect(returnUrl ?? Url.Content("~/"));
151
+
152
+
153
+
154
+ }
155
+ }
156
+
157
+ }
158
+
159
+ ```
160
+
46
161
  ### 試したこと
47
162
  Cookie認証を行った際のデータを使用してログインしたらユーザー名を表示するために
48
163
  ほかにもCookiePolicyやAuthenticationを使ってみたのですがどうにもうまくいきません。

1

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

2020/04/06 07:35

投稿

opo3939222
opo3939222

スコア6

title CHANGED
File without changes
body CHANGED
@@ -1,5 +1,4 @@
1
- コード
2
- ```### 前提・実現したいこと
1
+ ### 前提・実現したいこと
3
2
  ASP.Net Core 3.1でCookie認証を使ってログインを行うページを現在作っております。
4
3
  そこで、ログインに成功した際に右上にログインしたことが分かるようにログインユーザー名か
5
4
  ログイン中であることを表示することを通知するメッセージを作りたいと考えています。