簡體   English   中英

在 Blazor 服務器中獲取當前 Windows 用戶以進行 AD 身份驗證

[英]Getting current Windows user in Blazor Server for AD auth

我想使用 Blazor 服務器創建一個 Intranet 應用程序,其中登錄的 windows 用戶在訪問該站點時會自動針對“經典”AD 進行身份驗證。 就像單點登錄一樣。 我的問題是我無法訪問當前的 Windows 用戶。

我嘗試通過HttpContextAccessor但用戶是 null。 這是我的測試代碼。 稍后我計划使用此 doku 中的代碼進行身份驗證: Offical MS Doku

這甚至是正確的方法還是我必須編寫一個小的 Blazor wasm 應用程序來通過 signalR 連接將其移交?

public partial class Counter
{
    protected override void OnInitialized()
    {
        username = HttpContextAccessor.HttpContext.User.Identity.Name;

        if (String.IsNullOrWhiteSpace(username))
        {
             username = "user = null"; 
        }
    }

    private int currentCount = 0;

    private void IncrementCount()
    {
        currentCount++;
    }

    [Inject]
    private IHttpContextAccessor HttpContextAccessor { get; set; } = default!;

    private string username = default!;
}

我添加了services.AddHttpContextAccessor(); startup.cs

如果我理解正確,您可以在 IIS 服務器上啟用 windows 身份驗證
在 Visual Studio 的設計期間,在項目屬性-> 調試下,啟用 windows 身份驗證。

然后可以通過以下方式獲得身份:

var authState = await authenticationStateTask;
var user = authState.User;

if (user.Identity.IsAuthenticated)
..etc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM