小编Cod*_*Lee的帖子

如何使用 Blazor 服务器对用户进行身份验证

我有一个 Blazor 服务器应用程序,它使用 MongoDB 作为数据库,所以我试图用它来实现身份验证。所以我可以<Authenticted>, <AuthorizeView Roles="admin">在剃刀页面中使用和其他类似的标签。

内置的身份验证模板使用 SQL Server,在这种情况下我不想要,并且没有一个明确的示例说明如何自己使用另一个数据库来完成。鉴于微软在此处提供的示例

using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Components.Authorization;

namespace BlazorSample.Services
{
    public class CustomAuthStateProvider : AuthenticationStateProvider
    {
        public override Task<AuthenticationState> GetAuthenticationStateAsync()
        {
            var identity = new ClaimsIdentity(new[]
            {
                new Claim(ClaimTypes.Name, "mrfibuli"),
            }, "Fake authentication type");

            var user = new ClaimsPrincipal(identity);

            return Task.FromResult(new AuthenticationState(user));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应该如何在应用程序中使用它?您显然不会将单个值和值类型硬编码为唯一的身份验证来源。那么应该如何参数化呢?具有本地属性,例如:

Username { get; set; }
UserType { get; set; }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,你会在哪里设置?

另外,您将如何使用它来验证用户?我在ConfigurationServices(...)方法下的启动文件中添加了类:

...
services.AddScoped<AuthenticationStateProvider, MongoAuthenticationStateProvider>();
...
Run Code Online (Sandbox Code Playgroud)

我不知道如何验证任何人。我想你会以多种方式验证用户名和密码,然后当你知道它很好时,你继续更新 .NET 中的身份验证。我正在关注一个教程,他们在后面的代码中提出了类似的建议: …

c# authentication mongodb asp.net-core blazor

11
推荐指数
1
解决办法
1万
查看次数

Blazor 服务器页面的参数在手动刷新时抛出“404 not found”

我有一个 Blazor 服务器应用程序,其中一个页面接受 uri 中的参数。当我单击具有路由设置的锚标记以使用参数(如下所示)访问该页面时,链接工作正常,并且页面加载。

<a href="/MyPage/{@Param1}/{@Param2}"> <!--link content in here--> </a>
Run Code Online (Sandbox Code Playgroud)

但是,如果尝试直接从该 URL 访问该页面,或者在浏览器中手动刷新,则该页面不会重新初始化或命中参数上的任何断点。相反,它会抛出 404 not found。

所以这里有两件事:

  • 首先,我很困惑为什么它在锚标记内工作正常,但以其他方式消失。特别是当指令中没有参数的页面@page可以与刷新/直接网址一起正常工作时。
  • 其次,这是 Blazor Server 的预期行为还是我在这里遗漏了一些直接破坏页面刷新/点击 URL 的内容?看起来不像是一个功能,但也许我误解了 Blazor 的路由。

相关页面的 Razor 和 Razor.cs:

@page "/MyPage/{Param1}/{Param2}"

<h1>MyPage</h1>

<Metrics Param1="@Param1" />

<Details Param1="@Param1" Param2="@Param2" />

<InProgress Param1="@Param1" Param2="@Param2" />

<InQueue Param1="@Param1" />

<br />
Run Code Online (Sandbox Code Playgroud)
using System;
using System.Linq;
using System.Threading.Tasks;
using System.Collections.Generic;
using Microsoft.AspNetCore.Components;
using MyApp.Data.Models.ApiResponse;

namespace MyApp.Pages
{
    public partial class MyPage
    {
        [Parameter]
        public string Param1 { get; set; …
Run Code Online (Sandbox Code Playgroud)

c# page-refresh blazor blazor-server-side

8
推荐指数
1
解决办法
2954
查看次数

如何使用Perl6中的DBIish连接到本地MySQL Server 8.0

我正在研究一个Perl6项目,但很难连接到MySQL.即使使用DBIish(或perl6.org教程)示例代码,连接也会失败.任何建议或意见表示赞赏!用户凭据也已经过确认.

我在带有MySQL Server 8.0的Windows 10和带有Rakudo Star的标准Perl6上运行它.我尝试过多种方式修改连接字符串,例如:$ password:password <>:password()等但无法建立连接.还应注意我安装了ODBC,C,C++和.Net连接器.

#!/usr/bin/perl6
use v6.c;
use lib 'lib';
use DBIish;
use Register::User;

# Windows support
%*ENV<DBIISH_MYSQL_LIB> = "C:/Program Files/MySQL/MySQL Server 8.0/liblibmysql.dll"
    if $*DISTRO.is-win;

my $dbh = DBIish.connect('mysql', :host<localhost>, :port(3306), :database<dbNameHere>, :user<usernameHere>, :password<pwdIsHere>) or die "couldn't connect to database"; 
my $sth = $dbh.prepare(q:to/STATEMENT/);
    SELECT *
    FROM users
    STATEMENT

$sth.execute();

my @rows = $sth.allrows();

for @rows { .print }
say @rows.elems;

$sth.finish;
$dbh.dispose;
Run Code Online (Sandbox Code Playgroud)

这应该连接到DB.然后,应用程序运行查询,然后打印出每个结果行.实际发生的是应用程序每次都会点击"死"消息.

mysql dbi perl6

7
推荐指数
1
解决办法
208
查看次数

如何使用 Visual Studio Code 在 dotnet 构建中包含资源文件

我正在 Visual Studio 代码编辑器中使用 dotnet core 构建 WPF 应用程序。我遇到的问题是资源文件夹不包含在构建中,或者看起来是这样。我找不到如何在 Visual Studio 之外执行此操作的示例,并且我尝试使用的任何图像都不起作用。任何提示或示例将不胜感激!

文件夹结构

|- Root Folder
   |- bin
      |- Debug
         |- netcoreapp3.1
            |- [this has dlls and exe, but no Resources]
   |- obj
   |- Data
   |- Pages
   |- Resources
      |- images
Run Code Online (Sandbox Code Playgroud)

运行应用程序的命令:

|- Root Folder
   |- bin
      |- Debug
         |- netcoreapp3.1
            |- [this has dlls and exe, but no Resources]
   |- obj
   |- Data
   |- Pages
   |- Resources
      |- images
Run Code Online (Sandbox Code Playgroud)

这些都是标准内容,因此请指出任何丢失的命令、放错位置的文件等。我是 WPF 新手,正在尝试在 VS Code 中构建 Windows 应用程序。

c# wpf .net-core visual-studio-code

7
推荐指数
1
解决办法
1万
查看次数

如何在 .NET core 中从基于组织的身份设置/查找用户角色

我试图找出用户在 Blazor Server 应用程序中具有哪些角色,该应用程序为使用 MS 帐户和 Azure Active Directory 的组织设置了身份验证。我想这些角色是由管理 MS 帐户的 IT 运营团队设置的,但我没有找到在哪里验证这一点。

所以我想知道:使用 Org 身份验证时在哪里设置角色如何更改它们是否有一种简单的方法来查看经过身份验证的用户具有哪些角色

我尝试使用级联参数在MS doc示例代码中查找经过身份验证的用户角色,如下所示:

@page "/"

<button @onclick="LogUsername">Log username</button>

@code {
    [CascadingParameter]
    private Task<AuthenticationState> authenticationStateTask { get; set; }

    protected override void OnInitialized()
    {
        base.OnInitialized();
        LogUsername();
    }

    private async Task LogUsername()
    {
        var authState = await authenticationStateTask;
        var user = authState.User;

        if (user.Identity.IsAuthenticated)
        {
            var test = user.Claims.Where(c => c.Type.Contains("Role")); // also tried "role"
            string role = …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-identity blazor

6
推荐指数
1
解决办法
7478
查看次数

如何在安装时修复 Rust 柴油 cli 链接 libpq.lib 错误

我正在尝试(现在几个小时)diesel_cli为 postgres安装货箱。但是,每次我运行推荐的货物命令时:

cargo install diesel_cli --no-default-features --features postgres
Run Code Online (Sandbox Code Playgroud)

我等了几分钟才看到相同的构建失败 -> 中止错误,并显示以下消息:

note: LINK : fatal error LNK1181: cannot open input file 'libpq.lib'


error: aborting due to previous error

error: failed to compile `diesel_cli v1.4.1`, intermediate artifacts can be found at `C:\Users\<user name here>\AppData\Local\Temp\cargo-installUU2DtT`

Caused by:
  could not compile `diesel_cli`.
Run Code Online (Sandbox Code Playgroud)

我在 docker 容器中运行 postgres,并且在我C:\pgsqllibbin目录上都有二进制文件,PATH所以我无法弄清楚为什么它没有链接。他们在文档中没有提到的还有什么要求?

postgresql libpq rust rust-cargo rust-diesel

6
推荐指数
5
解决办法
2798
查看次数

如何同时检测浏览器和移动设备?JavaScript

我正在尝试使用 javascript 识别浏览器,以便我可以全屏播放视频或仅显示警报,我可以识别 chrome 和 safari 以及所有这些在笔记本电脑/台式机上都很好,这只是当我尝试还识别设备是否是移动它不起作用。我没有收到我想要的警报。我试过这个:https : //stackoverflow.com/a/3540295。但我没有运气,其中包括(原始答案,因为我不确定正则表达式是什么?):https : //stackoverflow.com/a/11381730
我现在有了这个。除非有更好的方法,否则我想使用用户代理。
JS:

function goFullscreen(id) {
  var element = document.getElementById(id); 
  var mobile = /Android|webOS|iPhone|iPad|iPod/i.test(navigator.userAgent);

  if (ua.indexOf('safari') != -1) { 
      if (ua.indexOf('chrome') > -1) {
        if (element.webkitRequestFullScreen) {
            if(mobile) {
                // some code for chrome mobile
                alert("chrome mobile")
            }else{
                //document.getElementById(id).classList.toggle("videoChange")
                alert("chrome desktop")
            }
        }
      } else if (element.msRequestFullscreen) {
            element.msRequestFullscreen(); //edge do somethig else
      } else if (element.mozRequestFullScreen) {
            element.mozRequestFullScreen(); //mozilla do somethig else  
      } else if (element.webkitRequestFullScreen) …
Run Code Online (Sandbox Code Playgroud)

javascript user-agent

5
推荐指数
1
解决办法
6455
查看次数

由于身份验证失败,无法将本地 git 推送到 Azure DevOps git 存储库,但密码正确

我有一个 Azure DevOps 帐户,我试图用它来托管 git 存储库。问题是,当我添加远程源然后尝试时,系统会git push -u origin --all提示我输入密码,但帐户的密码结果为:fatal: Authentication failed for...

是否需要设置不同的密码或需要配置不同的密码才能访问 Azure DevOps Repos?我找不到关于为什么您的帐户密码失败或如何设置另一个可接受的密码的合理解释。感谢您的任何提示或建议!

git azure azure-devops azure-repos

5
推荐指数
1
解决办法
1万
查看次数

EF Core - 值不能为空。(参数“键”)

我遇到的问题是 EF Core 不允许我添加第二次迁移。\n我有以下 DataContext 设置:

\n\n
public class DataContext : IdentityDbContext<User, Role, Guid, IdentityUserClaim<Guid>, UserRole, IdentityUserLogin<Guid>, IdentityRoleClaim<Guid>, IdentityUserToken<Guid>>\n{\n    public DataContext(DbContextOptions<DataContext> options)\n        : base(options)\n    {\n    }\n\n    /* Global */\n    public virtual DbSet<Audit> Audits { get; set; }\n    public virtual DbSet<AuditType> AuditType { get; set; }\n\n    /* Portal */\n    public virtual DbSet<RefreshToken> RefreshTokens { get; set; }\n    public virtual DbSet<Product> Products { get; set; }\n    public virtual DbSet<Group> Groups { get; set; }\n    public virtual DbSet<GroupType> GroupTypes { get; set; }\n …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework .net-core

5
推荐指数
1
解决办法
6259
查看次数

如何在带有 .NET 5.0+ 的 Linux 上使用 FileSystemWatcher

我正在尝试使用 FileSystemWatcher 类监视指定目录中的更改。很大程度上基于MS 文档上的示例,该示例在我的 Windows 计算机上运行良好。但问题是,我的目标是 Ubtuntu 18.04 lts 及更高版本来运行它。观察者显然设置得很好(代码执行并打印出控制台/日志消息),但没有任何文件系统事件被触发,即:更改/创建/删除/重命名/等没有任何事件触发这些事件

使用 Linux 是否需要任何未记录或“隐藏”的设置细节,或者这是否完全不受支持,即使它在 .NET 5 中也不应该用于 Linux/Mac/Unix 构建?我找不到任何关于非 Windows 使用的明确文档,但我认为 .NET 5 应该“可移植”地工作。任何提示/细节将不胜感激,因为我确信其他人也遇到过这个问题,或者至少有可能遇到过。

当前代码适用于 Windows 版本,但不适用于 Linux:

public void WatchFolder()
{
    string directoryPath = _configuration["FolderLocation"];

    if (string.IsNullOrWhiteSpace(directoryPath))
        throw new Exception("Folder location was not set.");

    try
    {
        // setup file system watcher
        using var watcher = new FileSystemWatcher(directoryPath);
        watcher.NotifyFilter = NotifyFilters.Attributes
                         | NotifyFilters.CreationTime
                         | NotifyFilters.DirectoryName
                         | NotifyFilters.FileName
                         | NotifyFilters.LastAccess
                         | NotifyFilters.LastWrite
                         | NotifyFilters.Security
                         | NotifyFilters.Size;

        // add handler …
Run Code Online (Sandbox Code Playgroud)

.net c# filesystemwatcher cross-platform .net-5

5
推荐指数
0
解决办法
4153
查看次数