MVC3

17

में कस्टम IPrincipal और IIdentity का उपयोग कर मैं अपने खुद के IPrincipal और IIdentity कार्यान्वयन के रूप में नीचे दिखाया गया है: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; } 
    } 

} 

इसके अलावा, मैं एक HttpModule और उसके AuthenticateRequest घटना में बनाते हैं, तो मैं यह कर:

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; 
    } 

इसके अलावा, मैं अपना खुद का Controller और WebViewPage बना देता हूं जैसे:

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; 
     } 
    } 
} 

जैसा कि उपरोक्त कोड में दिखाया गया है, ऐसा लगता है कि सब कुछ सही है; लेकिन जैसा कि आप देख सकते हैं, स्थान संख्या 1 मैं CustomPrincipal तक नहीं पहुंच सकता! इस जगह में, CustomPrincipal होने के बजाय मेरे पास RolePrincipal है। जैसे HttpContext.Current.UserCustomPrincipal के बजाय RolePrincipal है। लेकिन RolePrincipal.Identity संपत्ति CustomIdentity है!

उत्तर

19

आपका गलती यहाँ है:

_application.AuthenticateRequest += ApplicationAuthenticateRequest; 

एक HttpModule नामित RoleManagerModule कि HttpApplication.PostAuthenticateRequest में एक विधि का आह्वान और RolePrincipal करने के लिए HttpContext.Current.User सेट नहीं है। तो, आप User को AuthenticateRequest में सेट कर रहे थे और RoleManagerModule इसे PostAuthenticateRequest में सेट करता है, जिसका अर्थ है आपके सेट के बाद, आपकी सेटिंग्स को ओवरराइड करता है। बदलें अपनी Module.Init:

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

महत्वपूर्ण अपडेट:

कृपया this question स्टार्टर द्वारा -asked देख फिर से, एक दूसरे समाधान के लिए वर्तमान सवाल पर निर्भर करता है, तो यह एक काम नहीं करता।

+0

यह काम करता है! बहुत बहुत धन्यवाद। –

+0

इस उत्तर के लिए धन्यवाद; मैं इसे समझने की कोशिश कर अपने बालों को खींच रहा था! –

+0

@ डेविडकेवेनी धन्यवाद, लेकिन मुझे आपको बताना है कि समाधान में अभी तक कोई समस्या है। 'King.net' के प्रश्नों में खोजें, आप इस पर एक प्रश्न प्राप्त कर सकते हैं कि आईआईएस के साथ एक और समस्या है, इसलिए मैं सुझाव देता हूं कि वह इस मुद्दे को ठीक करे। कृपया 'king.net' के प्रश्नों को देखें, आपको यह मिल जाएगा। सादर। –