簡體   English   中英

帶有 Blazor 的 ASP.NET Core:無法弄清楚 cookie 身份驗證

[英]ASP.NET Core with Blazor: Cannot figure out cookie authentification

我試圖按照本教程( http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/4316/A-Demonstration-of-Simple-Server-side-Blazor-Cookie-Authentication.aspx )能夠使使用 ASP.NET Core 3.0 和 Blazor 框架 v.0.9.0(最新版本)進行 cookie 身份驗證的應用程序。 對於 IDE,我使用的是 VS 2019 預覽版。

作者使用較早版本的 Blazor,但我使用客戶端項目而不是 App 來遵循教程,因為我認為它們做同樣的事情,只是在不同版本的 Blazor 中調用不同。

這是我的結果的回購鏈接: https : //github.com/SaintMSent/TestBlazorCookieAuth

問題是,它不起作用。 如果我轉到 localhost:port/login 頁面,則會創建 cookie(我檢查了 Chrome 開發工具)。 如果我去注銷頁面,它會被刪除,所以這部分很好。 但應用程序的其他頁面不會加載。 如果我從 MainLayout 中刪除 Login 組件,一切都會正常加載,所以我認為問題出在 Login.cshtml 中的 HttpContext 和 HttpContextAccessor

在此處輸入圖片說明

這是 Login.cshtml 的樣子

@using System.Security.Claims
@using Microsoft.AspNetCore.Http
@page "/login"
@inject IHttpContextAccessor _httpContextAccessor
@inject HttpClient Http
@if (User.Identity.Name != null)
{
    <b>You are logged in as: @User.Identity.Name</b> 
    <a class="ml-md-auto btn btn-primary" 
       href="/logout?returnUrl=/" 
       target="_top">Logout</a>
}
else
{
    <a class="ml-md-auto btn btn-primary" 
       href="/login?returnUrl=/" 
       target="_top">Login</a>
}
@functions {
    private ClaimsPrincipal User;
    protected override void OnInit()
    {
        base.OnInit();
        try
        {
    // Set the user to determine if they are logged in
            User = _httpContextAccessor.HttpContext.User;
        }
        catch { }
    }
}

請幫我弄清楚這里發生了什么。 我希望我提供了足夠的細節,謝謝。

更新:如果我添加這一行

        services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();

到客戶端項目中的 Startup.cs,一切都在加載,但 _httpContextAccessor.HttpContext.User 始終為空

嘗試這樣做:

將以下內容添加到Blazor.Web.App.Startup.cs:

services.AddHttpContextAccessor();

您還需要在<component-name>.cshtml

@using Microsoft.AspNetCore.Http
@inject IHttpContextAccessor httpContextAccessor

編輯:您還應該從Server.Startup中刪除以下內容

 // As I've said above, add this to your Client.Startup, and not to the Server.Startup, though at the end of the day it's the same pipeline.
  services.AddHttpContextAccessor();
  // I'm not sure what is this for. Remove it :)
  services.AddScoped<HttpContextAccessor>();
  // This is used with Http Client Factory, right now is not supported in client-side blazor; requires some configuration; and in any case an HttpClient object is automatically created for you. 
  services.AddHttpClient();
  // Once again, superfluous, the HttpClient created for you is Singleton
  services.AddScoped<HttpClient>();

我以奇怪的方式解決了它。 我創建了一個名為AuthController的控制器,並將這些方法添加到其中

    [Route("api/[controller]")]
    public class AuthController : Controller
    {
        [HttpGet]
        [Route("getlogin")]
        public string GetLogin()
        {
            if (User.Identity.IsAuthenticated)
            {
                return $"{User.Identity.Name}";
            }

            return string.Empty;
        }

        [HttpGet]
        [Route("getrole")]
        public string GetRole()
        {
            if (User.Identity.IsAuthenticated)
            {
                if (User.IsInRole("Admin"))
                {
                    return "Admin";
                }

                return "User";
            }

            return string.Empty;
        }
    }

然后我從客戶端調用API,它對我來說非常有用

暫無
暫無

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

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