01-008 HttpAbstractions 之 ClaimsPrincipal 与 ClaimsIdentity
Posted on 2015-03-06 14:15 DotNet1010 阅读(236) 评论(0) 收藏 举报-
基于声明的 (Claims Based)
- ASP.NET Identity 支持“基于声明的身份验证 (claims-based authentication)”。这种机制使用一组“声明 (claims)”来表示用户的身份标识。比起角色,“声明”使得开发人员能够更好地描述用户的身份标识。基于声明的成员资格不同于基于角色的成员资格, 后者本质上只是一个布尔值(即“属于”或“不属于”特定角色),一个”声明“可以包含更多关于用户标识和成员资格的信息,信息的形式也更加丰富。
- ClaimsIdentity 由于 ASP.NET Identity 和 OWIN Cookie 身份验证是基于“声明” (claims) 的系统,所以框架要求应用程序为用户生成一个 ClaimsIdentity。ClaimsIdentity 包含有关于用户的所有声明信息,例如用户所属的角色。你也可以在这个阶段为用户添加更多的声明。
-
OWIN 集成
- ASP.NET 授权 (ASP.NET Authentication) 现在基于 OWIN 中间件 (middleware),可以在任何基于 OWIN 的宿主上使用。ASP.NET Identity 没有对 System.Web 的任何依赖,它完全兼容于 OWIN 框架,可以被用在任何由OWIN 承载的应用程序。
- ASP.NET Identity 使用 ASP.NET 授权 (ASP.NET Authentication) 为 Web 站点提供对用户的”登录/登出“操作。这意味着应用程序使用 CookieAuthentication 生成 cookie 而非 FormsAuthentication。
实现登录的三行代码:
private async Task SignInAsync()
{
// 1. 利用ASP.NET Identity获取用户对象
var user = await UserManager.FindAsync("UserName", "Password");
// 2. 利用ASP.NET Identity获取identity 对象
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
// 3. 将上面拿到的identity对象登录
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = true }, identity);
}
再看简单的登录代码代码:
protected void Application_AuthenticationRequest()
{
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, "myName"));
claims.Add(new Claim(ClaimTypes.Role, "Users"));
var identity = new ClaimsIdentity(claims, "myLogin");
ClaimsPrincipal principal = new ClaimsPrincipal(identity);
HttpContext.Current.User = principal;
}
IIdentity 接口代码:
//
// Summary:
// Defines the basic functionality of an identity object.
[ComVisible(true)]
public interface IIdentity
{
//
// Summary:
// Gets the type of authentication used.
//
// Returns:
// The type of authentication used to identify the user.
string AuthenticationType { get; }
//
// Summary:
// Gets a value that indicates whether the user has been authenticated.
//
// Returns:
// true if the user was authenticated; otherwise, false.
bool IsAuthenticated { get; }
//
// Summary:
// Gets the name of the current user.
//
// Returns:
// The name of the user on whose behalf the code is running.
string Name { get; }
}
IPrincipal 接口代码:
//
// Summary:
// Defines the basic functionality of a principal object.
[ComVisible(true)]
public interface IPrincipal
{
//
// Summary:
// Gets the identity of the current principal.
//
// Returns:
// The System.Security.Principal.IIdentity object associated with the current principal.
IIdentity Identity { get; }
//
// Summary:
// Determines whether the current principal belongs to the specified role.
//
// Parameters:
// role:
// The name of the role for which to check membership.
//
// Returns:
// true if the current principal is a member of the specified role; otherwise, false.
bool IsInRole(string role);
}
DefaultHttpContext 中:
public override ClaimsPrincipal User
{
get
{
var user = HttpAuthenticationFeature.User;
if (user == null)
{
user = new ClaimsPrincipal(new ClaimsIdentity());
HttpAuthenticationFeature.User = user;
}
return user;
}
set { HttpAuthenticationFeature.User = value; }
}
浙公网安备 33010602011771号