相关疑难解决方法(0)

从 StructureMap 获得的 HttpContext 上的空用户

好的,我之前的问题/设置有太多的变量,所以我将其分解为简单的骨骼组件。

鉴于以下使用 StructureMap3 的代码...

//IoC setup
For<HttpContextBase>().UseSpecial(x => x.ConstructedBy(y => HttpContext.Current != null ? new HttpContextWrapper(HttpContext.Current) : null ));
For<ICurrentUser>().Use<CurrentUser>();

//Classes used
public class CurrentUser : ICurrentUser
{
    public CurrentUser(HttpContextBase httpContext)
    {
        if (httpContext == null) return;
        if (httpContext.User == null) return;
        var user = httpContext.User;
        if (!user.Identity.IsAuthenticated) return;
        UserId = httpContext.User.GetIdentityId().GetValueOrDefault();
        UserName = httpContext.User.Identity.Name;
    }

    public Guid UserId { get; set; }
    public string UserName { get; set; }
}

public static class ClaimsExtensionMethods
    public static Guid? GetIdentityId(this IPrincipal …
Run Code Online (Sandbox Code Playgroud)

c# structuremap asp.net-mvc dependency-injection structuremap3

0
推荐指数
1
解决办法
3237
查看次数