本文介绍了在Asp.net核心MVC 3.1中的控制器内,HttpContext.User为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我使用Claim和在没有ASP.NET Core身份的情况下使用Cookie身份验证

In my application I sign-in with Claim and Use cookie authentication without ASP.NET Core Identity

为此,请在登录操作中使用以下代码,如此处

For that in login action use bellow code ,as describe here

var claims = new List<Claim>();

            foreach (var customclaim in customclaims)
            {
                claims.Add(new Claim(customclaim.Type, customclaim.Value));
            }

            var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);


            var authProperties = new AuthenticationProperties
            {
                //AllowRefresh = true,
                //ExpiresUtc = DateTimeOffset.Now.AddDays(1),
                //IsPersistent = true,
            };



            await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(claimsIdentity), authProperties);

            return new RedirectResult("~/home");

并像下面这样配置Startup.cs

and configure Startup.cs like bellow

public void ConfigureServices(IServiceCollection services)
        {


             services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
          .AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options =>
          {


              options.LoginPath = new PathString("/");
              options.LogoutPath = new PathString("/Account/Logout");
              options.AccessDeniedPath = options.LoginPath;
          });


            services.AddControllersWithViews(option =>
            {

            }) ;

            services.AddMvc();

        }


public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthentication();

            app.UseAuthorization();



            app.UseEndpoints(endpoints =>
            {
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "arearoute",
                        pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}");

                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Login}/{action=Index}/{id?}");
                });
            });
        }

,但是在HomeController中登录后, User 对象为null,并且通过null异常.

but after login in HomeController User object is null and through null exception.

如何从Controller类访问用户?

How can I access User from Controller class?

谢谢

推荐答案

我找到了答案以及在ASP.NET Core中访问HttpContext

这篇关于在Asp.net核心MVC 3.1中的控制器内,HttpContext.User为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 22:19