gpt4 book ai didi

c# - 在 ASP.NET MVC 3 中注入(inject) IPrincipal - 我做错了什么?

转载 作者:太空狗 更新时间:2023-10-30 01:24:02 24 4
gpt4 key购买 nike

我有一个自定义的 IIdentity 实现:

public class FeedbkIdentity : IIdentity
{
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }

public string Name { get; set; }

public FeedbkIdentity()
{
// Empty contructor for deserialization
}

public FeedbkIdentity(string name)
{
this.Name = name;
}

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

public bool IsAuthenticated
{
get { return !string.IsNullOrEmpty(this.Name); }
}
}

和自定义 IPrincipal

public class FeedbkPrincipal : IPrincipal
{
public IIdentity Identity { get; private set; }

public FeedbkPrincipal(FeedbkIdentity customIdentity)
{
this.Identity = customIdentity;
}

public bool IsInRole(string role)
{
return true;
}
}

在 global.asax.cs 中,我正在反序列化 FormsAuthenticationTicket userData 并替换

HttpContext.Current.User:

protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];

if (authCookie != null)
{
FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(authCookie.Value);

JavaScriptSerializer serializer = new JavaScriptSerializer();

FeedbkIdentity identity = serializer.Deserialize<FeedbkIdentity>(authTicket.UserData);

FeedbkPrincipal newUser = new FeedbkPrincipal(identity);
HttpContext.Current.User = newUser;
}
}

然后,在 Razor Views 中我可以:

@(((User as FeedbkPrincipal).Identity as FeedbkIdentity).FirstName) 

我已经在使用 Ninject 为成员(member)提供商注入(inject)自定义用户存储库,并且我一直在尝试将 IPrincipal 绑定(bind)到 HttpContext.Current.User:

internal class NinjectBindings : NinjectModule
{
public override void Load()
{
Bind<IUserRepository>().To<EFUserRepository>();
Bind<IPrincipal>().ToMethod(ctx => ctx.Kernel.Get<RequestContext>().HttpContext.User).InRequestScope();
}
}

但它不起作用。

我想要做的就是能够像这样访问我的自定义 IIdentity 属性:@User.Identity.FirstName

我怎样才能使这项工作?

编辑

我期望按照此处的建议将 IPrincipal 绑定(bind)到 HttpContext.Current.User:MVC3 + Ninject: What is the proper way to inject the User IPrincipal?我将能够在我的应用程序中访问 @User.Identity.FirstName

如果不是这种情况,如链接问题所示,将 IPrincipal 绑定(bind)到 HttpContext.Current.User 的目的是什么?

最佳答案

我建议创建您自己的基础 WebViewPage。参见 http://haacked.com/archive/2011/02/21/changing-base-type-of-a-razor-view.aspx

然后你可以在里面做

public FeedbkPrincipal FeedbkPrincipal
{
get
{
return User as FeedbkPrincipal;
}
}

public FeedbkIdentity FeedbkIdentity
{
get
{
return FeedbkPrincipal.Identity as FeedbkIdentity;
}
}

那就是

@FeedbkIdentity.FirstName

在 View 中。

what is the purpose of binding IPrincipal to HttpContext.Current.User as shown in the linked question

依赖注入(inject)让您可以在运行时选择您的组件。在给出的示例中,您可以这样做

public MyClassConstructor(IPrincipal principal) { // }

IPrincipal 将自动绑定(bind)到用户。请注意,这不会让您访问特定的 FeedbkPrincipal 属性,因为即使在运行时它会变成 FeedbkPrincipal 类型,您的类也不知道这一点。依赖注入(inject)不是解决当前问题的方法,因为您确实需要一种访问具体类的方法。

关于c# - 在 ASP.NET MVC 3 中注入(inject) IPrincipal - 我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10507937/

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