Asp.NET Core でいくつかのことを理解するのに苦労しています。ログイン認証を使用する Asp.NET 4.5 アプリケーションを既に持っていますFormAuthenticationTicket
が、私の目標は、ユーザーを認証し、4.5 アプリケーションが読み取るための Cookie を作成するコア Web Api をセットアップし、リダイレクト時に Cookie を介して既にサインインしていることです。 .
両方のアプリケーションに<machinekey>
web.config で同じものを指定し、に追加UseCookieAuthentication
しCookieAuthenticationOptions
ましたが、コア アプリケーションで内部Startup.cs
を複製する方法についてここから途方に暮れています。Core のドキュメントはまだあまり一貫性がありませんが、多くの提案を試してみましたが役に立ちませんでした。FormsAuthenticationTicket
ApplicationController.cs
私にとっての主な混乱は、コアでクッキーを作成できることだと思います。明らかに正しく作成していないか、正しく認証していない可能性が高いです。
Configure関数のStartup.cs
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationScheme = "ApiAuth",
CookieName = ".ASPXAUTH",
CookieHttpOnly = false,
ExpireTimeSpan = TimeSpan.FromDays(30),
SlidingExpiration = true,
AutomaticAuthenticate = true,
LoginPath = new PathString("/Application/Authorize"),
});
ApplicationController.cs
[HttpGet("Authorize/{appGuid}/{userGuid}", Name = "SignIn")]
public async Task<IActionResult> SignIn(Guid appGuid, Guid userGuid)
{
var application = Application.Find(appGuid);
var user = User.Find(userGuid);
if (application != null && user != null)
{
await HttpContext.Authentication.SignOutAsync("ApiAuth");
/****************Confusion start****************/
Claim cookiePath = new Claim(ClaimTypes.CookiePath, ".ASPXAUTH");
Claim expiration = new Claim(ClaimTypes.Expiration, DateTime.UtcNow.AddDays(30).ToString());
Claim expiryDate = new Claim(ClaimTypes.Expired, "false");
Claim persistant = new Claim(ClaimTypes.IsPersistent, "true");
Claim issueDate = new Claim("IssueDate", DateTime.UtcNow.ToString());
Claim name = new Claim(ClaimTypes.Name, user.Username);
Claim userData = new Claim(ClaimTypes.UserData, "");
Claim version = new Claim(ClaimTypes.Version, "2");
ClaimsPrincipal principal = new ClaimsPrincipal(new ClaimsIdentity(new[] { cookiePath, expiration, expiryDate,
persistant, issueDate, name, userData, version }, "ApiAuth"));
await HttpContext.Authentication.SignInAsync("ApiAuth", principal);
/****************Confusion end****************/
return new RedirectResult("http://localhost/MyWebsite/Repository.aspx");
}
return Unauthorized();
}
Cookie のサイズは、私の 4.5 アプリケーションのものよりもはるかに大きく、ここからどこへ行くべきか途方に暮れています。UseCookieAuthentication
ととの設定の競合も引き起こしていると思いますClaimsPrincipal
。