17

özel IPrincipal ve IIdentity kullanarak: Ayrıcaben kendi <code>IPrincipal</code> ve <code>IIdentity</code> uygulanmasını oluşturmak MVC3

[ComVisible(true)] 
[Serializable] 
public sealed class CustomIdentity : IIdentity { 

    private readonly string _name; 
    private readonly string _email; 
    // and other stuffs 

    public CustomIdentity(string name) { 
     _name = name.Trim(); 
     if(string.IsNullOrWhiteSpace(name)) 
      return; 
     _email = (connect to database and read email and other stuffs); 
    } 

    public string Name { 
     get { return _name; } 
    } 

    public string Email { 
     get { return _email; } 
    } 

    public string AuthenticationType { 
     get { return "CustomIdentity"; } 
    } 

    public bool IsAuthenticated { 
     get { return !string.IsNullOrWhiteSpace(_name); } 
    } 

} 


[ComVisible(true)] 
[Serializable] 
public sealed class CustomPrincipal : IPrincipal { 

    private readonly CustomIdentity _identity; 

    public CustomPrincipal(CustomIdentity identity) { 
     _identity = identity; 
    } 

    public bool IsInRole(string role) { 
     return _identity != null && 
       _identity.IsAuthenticated && 
       !string.IsNullOrWhiteSpace(role) && 
       Roles.IsUserInRole(_identity.Name, role); 
    } 

    IIdentity IPrincipal.Identity { 
     get { return _identity; } 
    } 

    public CustomIdentity Identity { 
     get { return _identity; } 
    } 

} 

, ben HttpModule ve AuthenticateRequest olay yaratmak, bunu:

public void Init(HttpApplication context) { 
     _application = context; 
     _application.AuthenticateRequest += ApplicationAuthenticateRequest; 
    } 

    private void ApplicationAuthenticateRequest(object sender, EventArgs e) { 
     var formsCookie = _application.Request.Cookies[FormsAuthentication.FormsCookieName]; 
     var identity = formsCookie != null 
      ? new CustomIdentity(FormsAuthentication.Decrypt(formsCookie.Value).Name) 
      : new CustomIdentity(string.Empty); 
     var principal = new CustomPrincipal(identity); 
     _application.Context.User = Thread.CurrentPrincipal = principal; 
    } 

public abstract class CustomController : Controller { 
    public new CustomPrincipal User { 
     get { 
      var user = System.Web.HttpContext.Current.User as CustomPrincipal; 
      return user; 
     } 
    } 
} 


public abstract class CustomWebViewPage<TModel> : WebViewPage<TModel> { 
    public new CustomPrincipal User { 
     get { 
      // (Place number 1) here is the error I'm speaking about!!! 
      var user = HttpContext.Current.User as CustomPrincipal; 
      return user; 
     } 
    } 
} 
:

Ayrıca, bu gibi kendi Controller ve WebViewPage oluşturmak Yukarıdaki kodda gösterildiği gibi

, her şey doğru gibi görünüyor; Ama görebildiğiniz gibi, Numara 1 numaralı telefondan CustomPrincipal'a erişemiyorum! Bu yerde anlamına gelir, CustomPrincipal yerine RolePrincipal var. Örneğin. HttpContext.Current.User, CustomPrincipal yerine RolePrincipal şeklindedir. Ancak RolePrincipal.Identity özelliği bir CustomIdentity!

cevap

19

Hatan buradadır:

_application.AuthenticateRequest += ApplicationAuthenticateRequest; 

HttpApplication.PostAuthenticateRequest bir yöntemi çağırır ve RolePrincipal için HttpContext.Current.User ayarlayan bir HttpModule adında RoleManagerModule yoktur. Yani, User'u AuthenticateRequest olarak ayarlıyorsunuz ve RoleManagerModule, PostAuthenticateRequest olarak ayarlıyor, setinizden sonra demektir, böylece ayarlarınızı geçersiz kılarsınız. Değiştir senin Module.Init:

public void Init(HttpApplication context) { 
    _application = context; 
    // change just this line: 
    _application.PostAuthenticateRequest += ApplicationAuthenticateRequest; 
} 

ÖNEMLİ GÜNCELLEME:

bu bir işe yaramazsa yine ikinci bir çözüm için geçerli sorduğu bağlıydı marş tarafından -asked this question bakınız.

+0

Çalışıyor! Çok teşekkürler. –

+0

Bu cevap için teşekkürler; Bunu anlamaya çalışırken saçlarımı çekiyordum! –

+0

@DavidKeaveny Teşekkür ederim, ama size çözümün henüz bir problemi olduğunu söylemeliyim. King.net'in sorularını araştırın, bununla bağlantılı bir Q'nun, IIS ile ilgili başka bir sorun olduğunu bulabilirsin, bu yüzden sorunu çözen bir yol öneririm. Lütfen king.net'in sorularına bakın, onu bulacaksınız. Saygılarımızla. –

İlgili konular