2016-06-10 8 views
0

Ich versuche, ein altes .NET 3.5, MVC 1 Projekt in ein neues .NET 4.5 MVC 5 Projekt zu migrieren. Ich bin in eine Straßensperre gerannt, in der sich eine alte Sicherheits-/Authentifizierungsinfrastruktur befindet, und ich bin mir nicht ganz sicher, wie ich diese auf die anspruchsbasierte Authentifizierung von OWIN umstellen kann. Ich versuche noch, FormsAuthentication mit OWIN beizubehalten. Ein paar Klassen:MVC 5- Änderung der Code-Infrastruktur von der alten Formularauthentifizierung zu OWIN Formularauthentifizierung

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 - verwendet DomainPrincipal Klasse AuthenticatedContext für eine Formularauthentifizierungsumgebung zu implementieren.

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 - Implementiert AuthenticatedContext für eine WCF-Service-Umgebung.

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 
} 

ich jetzt den Blick auf etwas zu erreichen ähnliche OWIN verwenden, die Unterzeichnung des Benutzers in etwa so:

// 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); 

und mit der benutzerdefinierten DomainPrincipal-Klasse anstelle als ClaimsPrincipal statt (es entsprechend zu ändern) und Ändern der Klassen WebFormsAuthenticatedContext und ServiceAuthenticatedContext, um mit diesem neuen Sicherheitsmodell "DomainPrincipal" und "OWIN" zu arbeiten.

Ist das möglich? Oder müsste ich die gesamte Authentifizierungsinfrastruktur komplett neu gestalten?

Ihre Hilfe und Ihr Rat würden uns sehr freuen.

Vielen Dank.

Antwort

0

Werfen Sie einen Blick auf diese blog Es gibt Ihnen möglicherweise mehr Einblick in die Owin-Authentifizierung zu implementieren.

Um Ihre Frage zu beantworten, wenn Sie nur einen benutzerdefinierten Prinzipal für die Behandlung von Rollen verwenden möchten - DomainPrinicpal (IPrincipal erweitern), müssen Sie nur den HttpContext.Current.User als Request.GetOwinContext() Request.User ersetzen in der WebFormsAuthenticatedContext-Klasse.

Verwandte Themen