4

この記事で説明されているように、ASP.NET Identity を使用せずに ASP.NET Core 1.0 で Cookie ミドルウェアを使用しています: https://docs.asp.net/en/latest/security/authentication/cookie.html

ユーザーが自分のプロファイルに特定の変更を加えた場合、Cookie のいくつかの値を変更する必要があります。このようなシナリオでは、この記事では次のように指示されています

context.ReplacePrincipal() を呼び出し、context.ShouldRenew フラグを true に設定します。

どうすれば正確にそれを行うことができますか?この記事は HttpContext について言及していると思います。HttpContext の下に ReplacePrincipal() メソッドが表示されません。

これについて少し助けていただければ幸いです。ありがとう。

4

1 に答える 1

4

この記事では、オプションでデリゲートCookieValidatePrincipalContextからを参照しています。OnValidatePrincipalCookieAuthenticationEvents

次のようにapp.UseCookieAuthentication関数に接続する必要があります。startup.cs

app.UseCookieAuthentication(options =>
{
     //other options here
     options.Events = new CookieAuthenticationEvents
     {
          OnValidatePrincipal = UpdateValidator.ValidateAsync
     };     
 });

関数は次のUpdateValidatorようになります。

public static class UpdateValidator
{
    public static async Task ValidateAsync(CookieValidatePrincipalContext context)
    {
        //check for changes to profile here

        //build new claims pricipal.
        var newprincipal = new System.Security.Claims.ClaimsPrincipal();

        // set and renew
        context.ReplacePrincipal(newprincipal);
        context.ShouldRenew = true;
    }
}

SecurityStampValidator github で見つけることができるクラスに良い例があります: https://github.com/aspnet/Identity/blob/dev/src/Identity/SecurityStampValidator.cs

于 2016-03-18T20:05:50.150 に答える