繁体   English   中英

在ASP.NET MVC 6(vNext)中Thread.CurrentPrincipal Identity.Name为null

[英]Thread.CurrentPrincipal Identity.Name is null in asp.net mvc 6 (vNext)

我正在使用Asp.Net MVC6(vNext),Identity 3.0。* beta3开发Web应用程序。 这是具有UI,服务和数据库的3层应用程序。 我能够使用Asp.net身份的SignInManager类登录用户,并且尝试在服务层中检索具有角色和声明的用户详细信息,以便可以验证用户是否有权执行某些操作。

我们如何从服务层中的当前请求主体身份获取当前用户名。 在MVC 5中,我已经使用Thread.CurrentPrincipal来做到这一点。 但是在MVC6中,将Thread.CurrentPrincipal Identity.Name设置为null。

有人可以让我知道我该怎么做吗? 另外,如果您有更好的解决方案,请告诉我。

如果您在Startup.cs中调用了AddIdentity()或在配置依赖项注入的任何位置,它将自动将单例HttpContextAccessor注册为满足IHttpContextAccessor服务的对象。

此时,在服务层中,您可以注入IHttpContextAccessor并从那里检索上下文。 这是假定您了解如何依赖注入系统的工作原理,并用它来实例化您的服务层类,而不仅仅是new荷兰国际集团他们。

如果您希望能够方便地访问HttpContext公开的ClaimsPrincipalIdName ,请确保导入System.Security.Claims以便可以访问为您提供GetUserName()GetUserId()扩展方法。

(这是先进的日期作为的beta4包被发布了VS2015 RC1是)

假设您可以在您的层中传递访问服务,则HttpContext.Current的等效项是IHttpContextAccessor.HttpContext ,它是托管层公开的服务。

可能有更好的方法。 但是现在,我将在下面的代码中添加一个自定义的中间件,以将ussername注入startup.cs类的当前线程中。 在服务层中,我将使用Thread.GetData(Thread.GetNamedDataSlot(“ current-username”))读取它。

public class Startup {

    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerfactory)
    {

        //.... code

        app.UseIdentity();

        //... more code

        app.Use(async (ctx, next) =>
        {
            var httpContext = (ctx as HttpContext);
            if (httpContext.User.Identity.IsAuthenticated)
            {
                Thread.SetData(Thread.GetNamedDataSlot("current-username"), httpContext.User.Identity.Name);
            }
            await next();
        });

        //... more code

        app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Index" });
            });

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM