C# ClaimsIdentity 登入
Web.config
Startup.cs
登入
登出
<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();
留言
張貼留言