में कस्टम 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.User
CustomPrincipal
के बजाय RolePrincipal
है। लेकिन RolePrincipal.Identity
संपत्ति CustomIdentity
है!
यह काम करता है! बहुत बहुत धन्यवाद। –
इस उत्तर के लिए धन्यवाद; मैं इसे समझने की कोशिश कर अपने बालों को खींच रहा था! –
@ डेविडकेवेनी धन्यवाद, लेकिन मुझे आपको बताना है कि समाधान में अभी तक कोई समस्या है। 'King.net' के प्रश्नों में खोजें, आप इस पर एक प्रश्न प्राप्त कर सकते हैं कि आईआईएस के साथ एक और समस्या है, इसलिए मैं सुझाव देता हूं कि वह इस मुद्दे को ठीक करे। कृपया 'king.net' के प्रश्नों को देखें, आपको यह मिल जाएगा। सादर। –