小编Mas*_*Loo的帖子

ASP.Net核心2:默认身份验证方案被忽略

我正在尝试在ASP.Net Core 2中构建自定义AuthenticationHandler。在跟进ASP.NET Core 2.0身份验证中间件以及为什么Asp.Net Core Authentication Scheme是强制性主题之后,我创建了特定的类。注册发生如下:

services.AddAuthentication(
    options =>
    {
        options.DefaultScheme = Constants.NoOpSchema;
        options.DefaultAuthenticateScheme = Constants.NoOpSchema;
        options.DefaultChallengeScheme = Constants.NoOpSchema;
        options.DefaultSignInScheme = Constants.NoOpSchema;
        options.DefaultSignOutScheme = Constants.NoOpSchema; 
        options.DefaultForbidScheme = Constants.NoOpSchema;
    }
).AddScheme<CustomAuthOptions, CustomAuthHandler>(Constants.NoOpSchema, "Custom Auth", o => { });
Run Code Online (Sandbox Code Playgroud)

如果特定的控制器明确设置了Scheme,则一切正常。

[Authorize(AuthenticationSchemes= Constants.NoOpSchema)]
[Route("api/[controller]")]
public class IndividualsController : Controller
Run Code Online (Sandbox Code Playgroud)

但是我不想设置模式,因为它应该动态添加。删除计划属性后,如下所示:

[Authorize]
[Route("api/[controller]")]
public class IndividualsController : Controller
Run Code Online (Sandbox Code Playgroud)

它不再起作用了。

我本来希望,设置DefaultScheme属性可以完成这项工作。有趣的是,我没有找到关于此主题的任何具体讨论。我在这里做错什么了吗?还是我的预期结果不对?

编辑:感谢您的问题,它对我有很大帮助。好像在没有CustomAuthHandler时,我仅使用的身份验证中间件正在映射DefaultScheme。因此,我必须始终添加AuthenticationMiddleware。

Edit2:不幸的是,它仍然不起作用。为了进一步说明我的问题:我将像往常一样添加中间件:

app.UseAuthentication();
app.UseMvc();
Run Code Online (Sandbox Code Playgroud)

现在,进入我的处理程序,它看起来像这样:

public class NoOpAuthHandler : AuthenticationHandler<NoOpAuthOptions>
{
    public const string NoOpSchema = "NoOp";

    public …
Run Code Online (Sandbox Code Playgroud)

c# authentication asp.net-core-mvc asp.net-core

9
推荐指数
2
解决办法
5132
查看次数

函数评估要求所有线程都运行-MVC

将值从模型传递到If语句中的参数时,发生以下错误。

QuickWatch错误说明

线程数

这是发生问题的代码,我很确定它不是ValidateUserPassword方法。

if (PSFNetSystem.ValidateUserPassword(model.Server, model.Username, model.Password) < 0)
{
    ModelState.AddModelError("Password", "Failed to login");
    return View(model);
}
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏,谢谢。

debugging asp.net-mvc

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

为什么脚手架没有按预期工作?

我正在尝试搭建脚手架,但出现以下错误:

运行所选代码生成器时出错:“没有为类型‘MvcProduct.Data.MvcProductContext’定义无参数构造函数。”

您可以在这里看到它的图像: 搭脚手架时出错

以下是我的MvcProductContext

using Microsoft.EntityFrameworkCore;
using MvcProduct.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MvcProduct.Data
{
    public class MvcProductContext : DbContext
    {
        public MvcProductContext(DbContextOptions<MvcProductContext> options)
            : base(options)
        {
        }

        public DbSet<Product> Product { get; set; }
    } 
Run Code Online (Sandbox Code Playgroud)

还有appsettings.json

 {
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MvcProductContext": "Server=(localdb)\\mssqllocaldb;Database=MvcProductContext-1;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
Run Code Online (Sandbox Code Playgroud)

ConfigureServices文件中的方法Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddControllersWithViews();

    services.AddDbContext<MvcProductContext>(options =>
    options.UseSqlServer(Configuration.GetConnectionString("MvcProductContext")));
}
Run Code Online (Sandbox Code Playgroud)

我还尝试在 …

c# model-view-controller entity-framework asp.net-mvc-scaffolding asp.net-core-mvc

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