gpt4 book ai didi

asp.net-mvc-3 - 在MVC3中使用自定义IPrincipal和IIdentity

转载 作者:行者123 更新时间:2023-12-03 11:56:50 26 4
gpt4 key购买 nike

我创建自己的IPrincipalIIdentity实现,如下所示:

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

另外,我创建自己的 ControllerWebViewPage像这样:
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!在这个地方,我有一个 RolePrincipal而不是 CustomPrincipal。例如 HttpContext.Current.UserRolePrincipal而不是 CustomPrincipal。但是 RolePrincipal.Identity属性是 CustomIdentity!

最佳答案

您的错误在这里:

_application.AuthenticateRequest += ApplicationAuthenticateRequest;

有一个名为 HttpModuleRoleManagerModule,它会调用 HttpApplication.PostAuthenticateRequest中的方法,并将 HttpContext.Current.User设置为 RolePrincipal。因此,您在 User中设置了 AuthenticateRequest,而 RoleManagerModulePostAuthenticateRequest中设置了它,意味着设置之后,因此将覆盖您的设置。更改您的 Module.Init:
public void Init(HttpApplication context) {
_application = context;
// change just this line:
_application.PostAuthenticateRequest += ApplicationAuthenticateRequest;
}

重要更新:

如果此方法不起作用,请再次查看 this question(由入门者再次询问,取决于当前问题)以寻求第二种解决方案。

关于asp.net-mvc-3 - 在MVC3中使用自定义IPrincipal和IIdentity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742259/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com