小编Rou*_*ace的帖子

Blazor WASM 尝试访问注册页面时授权失败:DenyAnonymousAuthorizationRequirement:需要经过身份验证的用户

我一直在 Blazor WASM 中创建一个项目并使用 Identity Server

一切正常,解决方案正在构建中。但是在某个阶段,我必须更改身份服务器上的设置,因为这现在会阻止未登录的用户注册或访问登录页面。

尝试导航到 https://localhost:44351/authentication/register 我在开发人员工具中收到以下控制台消息

info: Microsoft.AspNetCore.Authorization.DefaultAuthorizationService[2]
      Authorization failed. These requirements were not met:
      DenyAnonymousAuthorizationRequirement: Requires an authenticated user.
Run Code Online (Sandbox Code Playgroud)

据我所知,一切都相当标准

登录部分

@using Microsoft.AspNetCore.Identity
@using MDIS.SAW.Server.Models
@inject SignInManager<ApplicationUser> SignInManager
@inject UserManager<ApplicationUser> UserManager
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

@{
    var returnUrl = "/";
    if (Context.Request.Query.TryGetValue("returnUrl", out var existingUrl)) {
        returnUrl = existingUrl;
    }
}

<ul class="navbar-nav">
@if (SignInManager.IsSignedIn(User))
{
    <li class="nav-item">
        <a  class="nav-link text-dark" asp-area="Identity" asp-page="/Account/Manage/Index" title="Manage">Hello @User.Identity.Name!</a>
    </li>
    <li class="nav-item">
        <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="/" method="post">
            <button  type="submit" …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core blazor blazor-webassembly

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

IIDentity不包含"GetUserId"的定义

我正在尝试使用VS2015 MVC控制器,特别是User.Identity上的GetUserID

我已经看到了许多与此相关的类似问题,这些问题表明要添加对Microsoft.AspNet.Identity的引用,但是正如您所看到的那样,它被引用.

我假设我错过了一个包或其他东西,我无法弄明白

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Mvc;
using Web_Test.Models;  

namespace Web_Test.Controllers
{
    public class TestController : Controller
    {
    public TestController(UserManager<ApplicationUser> userManager, SignInManager<ApplicationUser> signInManager)
    {
        UserManager = userManager;
        SignInManager = signInManager;
    }
    public UserManager<ApplicationUser> UserManager { get; private set; }
    public SignInManager<ApplicationUser> SignInManager { get; private set; }

    public IActionResult Index()
    {
        //GetUserId() underlined in Red in VS2015
        //IIDentity does not contain a definition for 'GetUserId'
        string currentUserId = User.Identity.GetUserId();
        return View();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc visual-studio asp.net-core

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

如何处理空linq查询

我确信有一个更好的方式来写下面的内容会喜欢关于如何最好地解决它的一些指示

基本上我不能保证项目列表包含我之后和尝试根据所述项目返回值的特定项目.

class item
{
string Brand;
string Product;
decimal Value;
}

//List is created and filled elsewhere
List<item> _items;

void DoStuff()
{
decimal desiredValue = 0;

   try
     {
    var XXX = _items
             .Where(x=>x.Brand == "brand1")
             .Where(x=>x.Product == "product1")
             .First();

    desiredValue = XXX.Value;
     }
    catch()
     { 
     //Empty Catch Bugs me
     }
//Do something with desiredValue
   }
}
Run Code Online (Sandbox Code Playgroud)

c# linq

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

Linq Where声明表现缓慢

我有一个ListObjects(约10万),即必须以产生在迭代Dictionary.但是代码执行速度非常慢,特别是在一行上

public class Item{
        public int ID;
        public int Secondary_ID;
        public string Text;
        public int Number;
}
Run Code Online (Sandbox Code Playgroud)

数据看起来像(100k行)

ID  | Secondary_ID |      Text       | Number
1   |    1         | "something"     | 3
1   |    1         | "something else"| 7
1   |    1         | "something1"    | 4
1   |    2         | "something2"    | 344
2   |    3         | "something3"    | 74
2   |    3         | "something4"    | 1
Run Code Online (Sandbox Code Playgroud)

我希望它在完成后看起来像这样.(任何收藏都要诚实)

 Dictionary<int, string> 

Key             | Value
(secondary_ID) …
Run Code Online (Sandbox Code Playgroud)

c# linq

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

如何实现一个对象[]作为顶级对象始终是最新的?

我正在编写一个应用程序,我希望实现一个对象数组,它会定期添加一个新项目.

我总是想知道添加的最后一个对象是什么,并且正在考虑将该对象放在位置0(因此将每个项目按下一个索引到最多130个项目)

使用List使用它很容易实现

items.Insert(0,new item());
items.RemoveAt(130);
Run Code Online (Sandbox Code Playgroud)

然后将自动按下每个项目并在130处移除该项目,但这对于数组来说并不是那么简单

我最初想到的地方

for(int i = 129; i>0;i--)
{
 items[i] = items[i-1]; 
}
items[0] = new item();
Run Code Online (Sandbox Code Playgroud)

然后,这允许我简单地访问最新项目(通过索引[0]),并按创建顺序访问每个前面的项目(1 - > 129);

现在本身就相当简单,我想知道是否还有其他方法可以执行此操作.

编辑:感谢您的快速回复,

我已经对此进行了一些测试(使用了100万次迭代),看起来队列方法在这里是最快的,但只是稍微一点,然后列表然后数组花费了大约50%的时间来处理100万个项目

我想我将探索队列堆栈选项

再次感谢;

c# arrays xna

0
推荐指数
1
解决办法
103
查看次数