【问题标题】:ASP.NET Core using Impersonation cannot access User Identity使用模拟的 ASP.NET Core 无法访问用户身份
【发布时间】:2017-07-05 15:59:36
【问题描述】:

我正在使用 Windows 模拟在 Visual Studio Code 中构建一个 ASP.NET Core API (1.1) 进行身份验证。 (API 允许研究人员创建Samples。)我使用这个impersonation middleware 来处理身份验证,它在连接到底层SQL Server 数据库时很好地传递用户身份。

但是,对于某些写入操作,我想将创建对象的用户的名称作为值添加到数据库中(即创建样本的研究人员的名称)。我似乎无法让它工作。我的解决方案基于对这些问题的回答:How to get the current logged in user Id ASP.NET CoreASP.NET Core Identity does not inject UserManager<ApplicationUser> 和这个tutorial,尽管它们似乎旨在将用户身份存储在 SQL 服务器数据库中的单独表中,这不是我的意图.我只需要发送请求的用户的用户名。

我在控制器的var user = await GetCurrentUserAsync(); 行收到以下错误消息。

The 'await' operator can only be used within an async method. 
Consider marking this method with the 'async' modifier 
and changing its return type to 'Task<IActionResult>'

我的问题有两个:

  1. 如何解决此错误?

  2. 在我的情况下,是否有更简单/更好的方法来获取用户身份。

我的Controller 文件

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using System.Security.Claims;
using MyAPI.Model;
using MyAPI.Services;
namespace MyAPI.Controllers
{
    [Route("api/[controller]")]
    public class SamplesController : Controller
    {
        private readonly UserManager<ApplicationUser> _userManager;
        private Task<ApplicationUser> GetCurrentUserAsync() => _userManager.GetUserAsync(HttpContext.User);

        [HttpPost]
        public IActionResult Post([FromBody] Sample sample)
        {
            var user =  await GetCurrentUserAsync();
            var userId = user?.Id;
            // I abstracted the underlying logic of creating a sample in the database
            //because it is quite complex and doesn't seem relevant to this problem
            CreateSample(sample, userId);
        }
    }
}

Startup.cs文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.EntityFrameworkCore;
using MyAPI.Model;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Impersonate.AspNetCore.Windows;
namespace MyAPI
{
    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(env.ContentRootPath)
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
            Configuration = builder.Build();
        }

        public IConfigurationRoot Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddIdentity<ApplicationUser, IdentityRole>()
                .AddEntityFrameworkStores<ApplicationDbContext>()
                .AddDefaultTokenProviders();
            // Add framework services.
            services.AddMvc();

        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
        {
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            loggerFactory.AddDebug();
            app.UseWindowsImpersonation(options => { options.Enabled = true; });
            app.UseMvc();

        }
    }
}

MyAPI.Model.ApplicationDbContext文件

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace TrinityAPI.Model
{
  public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
  {
    public ApplicationDbContext()
    {
      Database.EnsureCreated();
    }
  }
}

MyAPI.Model.ApplicationUser文件

using Microsoft.AspNetCore.Identity.EntityFrameworkCore;

namespace TrinityAPI.Model
{
  public class ApplicationUser : IdentityUser
  {
  }
}

【问题讨论】:

    标签: c# asp.net-core impersonation asp.net-core-1.1


    【解决方案1】:

    启用 Windows 身份验证并在控制器操作的代码中,您可以通过转到 HttpContext 属性上的 User 对象来查找有关当前用户的信息。例如,以下操作应显示当前用户的域\用户名。

    public IActionResult Index()
    {
        return Content(HttpContext.User.Identity.Name);
    }
    

    在使用 Windows 身份验证时,您认为您不想使用 ASP.NET Identity 是正确的。您可以删除 ApplicationDbContext 类以及 Startup 类中的 AddIdentity 调用。

    【讨论】:

      猜你喜欢
      • 2017-10-30
      • 2014-06-28
      • 2019-03-11
      • 2019-02-11
      • 2011-04-06
      • 1970-01-01
      • 2021-06-23
      • 2020-04-29
      • 2011-12-11
      相关资源
      最近更新 更多