【发布时间】:2016-10-10 20:48:53
【问题描述】:
我正在尝试将旧的 .NET 3.5、MVC 1 项目迁移到新的 .NET 4.5 MVC 5 项目。我遇到了一个障碍,那里有旧的安全/身份验证基础设施,我不太确定如何将其更改为基于 OWIN 声明的身份验证。我仍在尝试使用 OWIN 维护 FormsAuthentication。几个类:
DomainPrincipal.cs
public sealed class DomainPrincipal : MarshalByRefObject, IPrincipal
{
private readonly IIdentity _identity;
private readonly User _user;
/// <summary>
/// Initializes a new instance of the <see cref="DomainPrincipal"/> class.
/// </summary>
/// <param name="identity">The identity.</param>
/// <param name="user">The user.</param>
public DomainPrincipal(IIdentity identity, User user)
{
_identity = identity;
_user = user;
}
/// <summary>
/// Gets the user.
/// </summary>
/// <value>The <see cref="User"/> associated with the current principal.</value>
public User User
{
get { return _user; }
}
#region IPrincipal Members
/// <summary>
/// Determines whether the current principal belongs to the specified role.
/// </summary>
/// <returns>
/// <c>true</c> if the current principal is a member of the specified role; otherwise <c>false</c>.
/// </returns>
/// <param name="roleName">The name of the role for which to check membership. </param>
public Boolean IsInRole(String roleName)
{
return User.HasPrivilege(roleName);
}
/// <summary>
/// Gets the identity of the current principal.
/// </summary>
/// <value></value>
/// <returns>The <see cref="T:System.Security.Principal.IIdentity"/> object associated with the current principal.</returns>
public IIdentity Identity
{
get { return _identity; }
}
#endregion
}
WebFormsAuthenticatedContext.cs - 使用 DomainPrincipal 类为表单身份验证环境实现 AuthenticatedContext。
public sealed class WebFormsAuthenticatedContext : AuthenticatedContext
{
/// <summary>
/// Gets the current user.
/// </summary>
/// <value>The current user.</value>
/// <remarks>Determines the current user from the user principal of the current <see cref="HttpContext"/>.</remarks>
public User User
{
get { return IsValid ? ((DomainPrincipal)HttpContext.Current.User).User : null; }
}
/// <summary>
/// Gets a value indicating whether the <see cref="User"/> property is currently valid.
/// </summary>
/// <value>
/// <c>true</c> if the <see cref="User"/> property is valid; otherwise, <c>false</c>.
/// </value>
public Boolean IsValid
{
get { return HttpContext.Current != null && HttpContext.Current.User is DomainPrincipal; }
}
}
ServiceAuthenticatedContext.cs - 为 WCF 服务环境实现 AuthenticatedContext。
public class ServiceAuthenticatedContext : AuthenticatedContext
{
/// <summary>
/// Gets or sets the user DAO.
/// </summary>
/// <value>The user DAO.</value>
public UserDao UserDao { get; set; }
#region AuthenticatedContext Members
/// <summary>
/// Gets the current user.
/// </summary>
/// <value>The current user.</value>
/// <remarks>Determines the user identity from the current <see cref="ServiceSecurityContext"/>.</remarks>
public User User
{
get
{
const string AuthorizationContextKey = "DomainPrincipal";
AuthorizationContext authorizationContext = ServiceSecurityContext.Current.AuthorizationContext;
User user;
if (authorizationContext.Properties.ContainsKey(AuthorizationContextKey))
{
user = authorizationContext.Properties[AuthorizationContextKey] as User;
}
else
{
string username = ServiceSecurityContext.Current.PrimaryIdentity.Name;
user = UserDao.GetByUsername(username);
authorizationContext.Properties.Add(AuthorizationContextKey, user);
}
return user;
}
}
public String Username { get; private set; }
/// <summary>
/// Gets a value indicating whether the <see cref="User"/> property is currently valid.
/// </summary>
/// <value>
/// <c>true</c> if the <see cref="User"/> property is valid; otherwise, <c>false</c>.
/// </value>
public bool IsValid
{
get { return ServiceSecurityContext.Current.PrimaryIdentity != null && ServiceSecurityContext.Current.PrimaryIdentity.IsAuthenticated; }
}
#endregion
}
我现在正在考虑使用 OWIN 实现类似的功能,像这样让用户登录:
// Log user in
var identity = new ClaimsIdentity(new[]
{
new Claim(ClaimTypes.Name, model.Username),
},
DefaultAuthenticationTypes.ApplicationCookie,
ClaimTypes.Name, ClaimTypes.Role);
Authentication.SignIn(new AuthenticationProperties
{
IsPersistent = model.RememberMe
}, identity);
并使用自定义的 DomainPrincipal 类作为 ClaimsPrincipal 代替(相应地更改它)并更改 WebFormsAuthenticatedContext 和 ServiceAuthenticatedContext 类以使用这个新的 DomainPrincipal 和 OWIN 安全模型。
这可能吗?还是我需要彻底改造整个身份验证基础架构?
非常感谢您的帮助和建议。
谢谢。
【问题讨论】:
标签: c#-4.0 owin asp.net-mvc-5