動くコード図鑑技術記事現場の渡り方キャリア論すべての記事About
C#

定石3: Cookie認証— OWIN + ASP.NET Identity

出典: 業務イントラの認証 — Windows認証 / Forms認証 / Cookie の使い分けで業務SE が踏む選択定石3: Cookie認証— OWIN + ASP.NET Identity

定石3: Cookie認証— OWIN + ASP.NET Identity (csharp)#646899dc7794
// ✅定石3: OWIN Startup.csでのCookie認証設定
using Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.AspNet.Identity;
 
[assembly: OwinStartup(typeof(MyApp.Startup))]
 
public partial class Startup
{
    public void ConfigureAuth(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Account/Login"),
            ExpireTimeSpan = TimeSpan.FromMinutes(60),
            SlidingExpiration = true,
            CookieSecure = CookieSecureOption.Always,   // SSL必須
 
            Provider = new CookieAuthenticationProvider
            {
                OnValidateIdentity = SecurityStampValidator
                    .OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
                        validateInterval: TimeSpan.FromMinutes(30),
                        regenerateIdentity: (manager, user)=>
                            user.GenerateUserIdentityAsync(manager))
            }
        });
 
        //外部プロバイダ追加(Microsoft, Google, etc.)
        // app.UseMicrosoftAccountAuthentication(...);
        // app.UseGoogleAuthentication(...);
    }
}
▸ この snippet は実行結果未収録
▸ 実行結果は未収録です
  • id: #646899dc7794
  • lines: 35
  • extracted: 2026-06-10

Source収録記事

この snippet は記事の「定石3: Cookie認証— OWIN + ASP.NET Identity」セクションに登場する。コードの前後の文脈・ハマりどころの解説は記事本文で。

同じ記事から

4
図鑑トップ