English 中文(简体)
在MVC3中,使用自定义的一等和二等身份3
原标题:using custom IPrincipal and IIdentity in MVC3

我创建了自己的 Iproduct identity 实施如下:

[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 ,并且在其 认证请求 事件中,我这样做:

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

此外,我还创建了我自己的 current 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;
        }
    }
}

上面的代码显示, 一切似乎都是正确的; 但您可以看到, 在 < strong> Place编号 1 中, 我无法访问 < code> Custom proper ! 此处的意思是, 我有一个 < code > Role proper , 而不是一个 < code > HttpContext.crent. user 。 但 < code> Custom proper 属性是 < code> Customity !

最佳回答

你的错误在这里:

_application.AuthenticateRequest += ApplicationAuthenticateRequest;

HttpModule 命名为 RoleManagerModule ,在 HtpApplication application.PostAuthenticateExpress 中引用了一种方法。 PostAuthenticature 并设置了 HtpContext.Current.User RoleBloomel 。因此,你在 Audenticate 中设置了 ,在 RoleManagerModule 中设置了一种方法,在 PostAnnexctection 的意思是,在设置后修改您的设置。

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

<强 > 重要更新:

请见https://stackoverflow.com/ questions/11001061/producer-dossant-work- with-cust-identity-and-iprincipal-on-server> -再次被启动者问醒,取决于当前的问题-如果第二个解决方案不起作用,则第二个解决方案取决于当前的问题。

问题回答

暂无回答




相关问题
c# FormsAuthentication signOut another user

I m using formsAuthentication and as an admin user to the site i would like to be able to sign another user out. Is this possible ?

Hosted Silverlight LOB Application - Authentication Models

Our application is built in VB6 and delivered in a SaaS model via Citrix. Our subscribers must first authenticate to the Citrix Login Portal (AD) which gives them access to their applications. Each ...

ASP.Net Session Not Invalidated After Logout

I have a ASP.Net application in my login page I call FormsAuthentication.SignOut Session.Abandon() Session.Clear() however the Appscan is taking the ASPXAUTH cookie value then after logout is ...