C# .Net Core ASP.Net API 用户为空

肯尼斯·比恩

我有一个基本的 C# 2.0 .Net Core API。在控制器内部,我调用 User.Identity 来获取登录用户的信息。我将 IdentityServer 4 与 Jwt Bearer 身份验证一起使用。问题User是空的。然而,在 NLog 输出中,它正确地找到了我的名字并说我登录了。我尝试发送错误的令牌,但它正确地拒绝了我。

017-12-14 11:21:57.6788||INFO|Microsoft.AspNetCore.Hosting.Internal.WebHost|Request starting HTTP/1.1 GET http://localhost:62150/api/v1/user   
    2017-12-14 11:21:58.9575||INFO|Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler|Successfully validated the token. 
    2017-12-14 11:21:58.9637||INFO|Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler|AuthenticationScheme: BearerIdentityServerAuthenticationJwt was successfully authenticated. 
    2017-12-14 11:21:58.9637||INFO|IdentityServer4.AccessTokenValidation.IdentityServerAuthenticationHandler|AuthenticationScheme: Bearer was successfully authenticated. 
    2017-12-14 11:21:59.0217||INFO|Microsoft.AspNetCore.Authorization.DefaultAuthorizationService|Authorization was successful for user: Ken. 
    2017-12-14 11:21:59.1724||INFO|Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|Executing action method Controllers.UserController.Index with arguments ((null)) - ModelState is Valid 
    2017-12-14 11:22:01.7092||INFO|Microsoft.AspNetCore.Mvc.Internal.ControllerActionInvoker|Executed action Controllers.UserController.Index in 2690.9354ms 
    2017-12-14 11:22:01.7548||ERROR|Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware|An unhandled exception has occurred while executing the request Object reference not set to an instance of an object.

[Route("api/v1/[controller]")]
    [Authorize]
    public class UserController : ApiController
    {
        /// <summary>
        /// GET /user
        /// </summary>
        /// <remarks>Get information about the current logged in user.  Required Authorization.</remarks>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Index()
        {
            var identity = this.User.Identity.GetUserGuid();
        }
    }




// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseCors("default");
            app.UseAuthentication();
            app.UseMvc();
        }

 // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvcCore()
                .AddAuthorization()
                .AddJsonFormatters();

            services.AddMemoryCache();

            services.AddCors(o => o.AddPolicy("default", builder =>
            {
                builder.AllowAnyOrigin()
                       .AllowAnyMethod()
                       .AllowAnyHeader();
            }));

            JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
            services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme)
                .AddIdentityServerAuthentication(options =>
                {
                    options.Authority = "http://localhost:5000"; // Auth Server
                    options.RequireHttpsMetadata = false; // only for development
                    options.ApiName = "webApi"; // API Resource Id
                    options.SaveToken = true;
                });
        }
肯尼斯·比恩

问题是因为我正在调用 ApiController 而不是新的 Mvc BaseController。问题解决了。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

JWT Bearer ASP.Net Core 3.1用户在服务器上为空

来自分类Dev

将 Json 对象从 Angular 解析为 C# ASP.Net Core Web API

来自分类Dev

C#ASP.NET Core Web API包含在何处

来自分类Dev

ASP.NET Core API请求的空对象检查

来自分类Dev

调试asp.net core web API

来自分类Dev

IFormFile在Asp.Net Core WebAPI中始终为空

来自分类Dev

ASP.Net身份,角色中的用户始终为空

来自分类Dev

ASP.NET Core 路由空参数

来自分类Dev

Asp.net Core 用户可选 CSS

来自分类Dev

我如何使用 c# 在 asp.net web api(不是 .net core)中实现 api 网关

来自分类Dev

ASP.NET Core API不接受可为空类型的空值

来自分类Dev

asp.net core web api如何从客户端发送json对象c#

来自分类Dev

在 c# asp.net core web api 中创建 jwt 令牌

来自分类Dev

ASP.Net Core API:如何将用户身份链接到联系人

来自分类Dev

ASP.NET Core,以查询字符串为模板的Web API RouteAttribute

来自分类Dev

后缀为“ Z”时,本地时间未解析ASP.NET Core API ISO8601

来自分类Dev

ASP.NET Core API中的模型绑定始终为null

来自分类Dev

ASP.NET 和 EF Core Web API 为对象中的对象返回 null

来自分类Dev

Asp Net Core Web Api POST Request.Body 总是长度为 0

来自分类Dev

从ASP.NET Core 3.1升级到ASP.NET 5.0后,User.Claims为空

来自分类Dev

使用Web API的ASP.Net Core路由

来自分类Dev

ASP.NET Core 1.0 Web API不返回XML

来自分类Dev

如何从Web API调用ASP.NET Core Web MVC

来自分类Dev

在Asp.net Core中将字典发布到Web API

来自分类常见问题

使用VSTS的ASP.NET Core Web API的CI / CD

来自分类Dev

ASP.NET Core Web Api自动帮助页面

来自分类Dev

ASP.NET Core 1.0 Web API使用camelcase

来自分类Dev

使用ASP.NET Core定义API调用程序类

来自分类Dev

Odata ASP.NET Core 2.2 Web API分页

Related 相关文章

热门标签

归档