2017-07-05 46 views
0

我正在使用Windows模擬進行身份驗證的Visual Studio代碼中構建ASP.NET Core API(1.1)。 (該API允許研究人員創建Samples。)我使用此impersonation middleware來處理身份驗證,該身份驗證在連接到基礎SQL Server數據庫時很好地傳遞用戶身份。使用模擬的ASP.NET核心無法訪問用戶標識

但是,對於某些寫操作,我想將創建該對象的用戶的名稱作爲值添加到數據庫(即創建示例的研究員的姓名)。我似乎無法使其工作。我的解決方案基於對這些問題的回答: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 
    { 
    } 
} 

回答

2

啓用Windows身份驗證並在控制器操作的代碼中,可以通過轉到HttpContext屬性上的User對象來查找有關當前用戶的信息。例如,以下操作應顯示當前用戶的域\用戶名。

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

當使用Windows身份驗證時,您認爲您不想使用ASP.NET身份正確。您可以刪除ApplicationDbContext類以及Startup類中的AddIdentity調用。

相關問題