私が学んだことのほとんどは、this microsoft docから来ていますが、あなたが書いているように、ここまであなたを連れていかないようです。
startup.csで
public void ConfigureServices(IServiceCollection services)
{
...
services.AddAuthentication("ACE_AUTH")
.AddCookie("ACE_AUTH", options => {
options.AccessDeniedPath = "/api/Auth/Forbidden";
options.LoginPath = "/";
options.Cookie.Expiration = new TimeSpan(7,0,0,0);
});
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env,
ILoggerFactory loggerFactory)
{
...
app.UseAuthentication();
}
そして、認証を処理し、あなたのコントローラに:あなたは、認証要求の結果を認証し、LOGGEDINを割り当てることができる場合
[HttpPost()]
[Route("api/[Controller]/[Action]/")]
public async Task<JsonResult> Login([FromBody]Dictionary<string, string> loginData)
{
try
{
var loggedIn = true;
if (loggedIn)
{
var claims = new List<Claim> {
new Claim(ClaimTypes.Name, "John Doe")
};
var identity = new ClaimsIdentity(CookieAuthenticationDefaults.AuthenticationScheme);
identity.AddClaims(claims);
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
await HttpContext.SignInAsync(
"ACE_AUTH",
principal,
new AuthenticationProperties
{
IsPersistent = true,
ExpiresUtc = DateTime.UtcNow.AddDays(7)
});
}
return new JsonResult(logRtn);
}
catch (Exception ex)
{
return new JsonResult(ex.Message);
}
}
、次のことができるようにすべきですクッキーにクレームを格納する。次に、次のコードを使用して権限/リコール値を実行している可能性のあるコントローラのクレームを思い出すことができます。
[HttpGet("[Action]", Name = "GetSomething")]
[Route("[Action]")]
public JsonResult something()
{
try
{
var loggedInUser = HttpContext.User;
var claym = loggedInUser.Claims.FirstOrDefault(x => x.Type == ClaimTypes.Name);
if (claym != null)
{
return new JsonResult(claym.Value);
// returns "John Doe"
}
else
{
return new JsonResult("");
}
}
catch (Exception ex)
{
return new JsonResult(ex.Message);
}
}
同じ問題に直面しています。あなたはそれを把握しましたか? – Clement
まだありません。私は今のところ他のものに移行しなければなりませんでしたが、私はまもなくそれに戻るでしょう。 –