C# ClaimsIdentity 登入

Web.config
<configuration>
    <appSettings>
        <add key="owin:AppStartup" value="APP.App_Start.Startup" />
    </appSettings>
</configuration>


Startup.cs
namespace APP.App_Start
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
                AuthenticationMode = AuthenticationMode.Active,
                LoginPath = new PathString("/Account/LogOn"),
                ExpireTimeSpan = TimeSpan.FromDays(365),
                CookieName="StuAppAuth",
                CookieHttpOnly=true
            });
        }
    }
}

登入
var claim = new List<Claim> {
   new Claim(ClaimTypes.Name, "test1"),
   new Claim(ClaimTypes.UserData, "test2"),
   new Claim(ClaimTypes.Sid, "test3"),
   new Claim("Dept", "test4"),
   new Claim(ClaimTypes.NameIdentifier, "test5"),
   new Claim(ClaimTypes.Role, "test6"),
   new Claim(ClaimTypes.Role, "test7")
};

var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);


var authenticationManager = HttpContext.GetOwinContext().Authentication;
authenticationManager.SignIn(
   new AuthenticationProperties
   {
      IsPersistent = true
   },
identity);

登出
Request.GetOwinContext().Authentication.SignOut();

留言

這個網誌中的熱門文章

Android WebView 發生跑版

Entity Delete 因為實體未公開其關聯性的外部索引鍵屬性而發生錯誤

OS X Yosemite 10.10.1 + Apache + PHP + MariaDB