相关疑难解决方法(0)

获取ASP.NET Identity 2.0中的当前用户标识

我刚刚切换到使用Identity 2.0的新2.0版本.在1.0中我可以使用获取用户对象manager.FindByIdAsync(User.Identity.GetUserId()).该GetUserId()方法似乎不存在于2.0中.

现在我可以弄清楚的是使用manager.FindByEmailAsync(User.Identity.Name)哪些引用users表中的用户名字段.在我的应用程序中,它设置为与电子邮件字段相同.

当有人需要更新他们的电子邮件时,我可以看到这会导致问题.有没有办法根据Identity 2.0 Framework中不变的值(例如id字段)获取当前登录的用户对象?

c# asp.net asp.net-identity

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

如何使用ASP.NET Core RC2 MVC6和IIS7获取当前Windows用户

我有一个使用ASP.NET Core RC2在MVC6中构建的Intranet站点.我想获取访问Intranet站点的人的Windows用户名.

因此,如果Jim访问内部网站点,我希望该站点接收"Domain\Jim",而如果Anne访问内部网站,我希望该站点接收"Domain\Anne".

我的IIS服务器上仅启用了Windows身份验证,禁用了匿名身份验证.

我的网站设置为使用Windows身份验证并禁用匿名身份验证.

<system.web>
    <authentication mode="Windows" />
    <authorization>
        <deny users="?"/>
    </authorization>
</system.web>
Run Code Online (Sandbox Code Playgroud)

通过调试我可以使用System.Security.Principal.WindowsIdentity.GetCurrent().Name,但当然在IIS服务器上返回"IIS APPPOOL\SiteName".

我使用HttpContext从旧版本的ASP.NET中找到了许多示例,我尝试使用以下内容将其注入到我的控制器中,但userName最终为null.

//Startup.cs
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();

//HomeController.cs
public HomeController(IHttpContextAccessor _accessor)
{
    string userName = _accessor.HttpContext.User.Identity.Name;
}
Run Code Online (Sandbox Code Playgroud)

将Windows用户名传递给ASP.NET Core RC2 MVC6中的Intranet站点的正确方法是什么?

c# iis-7 asp.net-core-mvc asp.net-core .net-core-rc2

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

如何使用asp.net Identity 2.0授权用户只能查看自己的记录

对于这样一个普遍的问题一定有一个简单的解决方案,所以我为我的无知提前道歉:

我有一个多用户 Web 应用程序(带有 EF6 的 Asp.net MVC5),允许用户查看和/或修改存储在多个相关表(Company、Csearch、Candidate)中的相关数据。(更多详情请参见下文)。他们不应该看到任何其他数据(例如通过篡改 URL)。

我使用 Asp.net Identity 2.0 进行身份验证,并且也想将其用于上述授权。用户数据存储在标准的 AspNetUser 表中。我仅对身份表和业务表使用一种上下文。

我想我必须使用角色或声明来解决这个问题,但我找不到任何关于如何做到这一点的指导。有人能指出我正确的方向吗?

我目前已经通过向 CompanyController 添加 LINQ 条件来解决它(对于公司模型),但这似乎不是解决问题的非常安全和正确的方法。

public ActionResult Index(int? id, int? csearchid)
        {
            var companies = db.Companies
              .OrderBy(i => i.CompanyName)
              .Where(t => t.UserName == User.Identity.Name);
        return View(companies);
Run Code Online (Sandbox Code Playgroud)

我的数据模型很简单,我使用 Visual Studio 2017 搭建了它的脚手架,首先通过 EF6 代码,我构建了一个关系数据模型,大致如下:

一个公司可以有多个搜索(一对多)。每个搜索可以有多个候选者(一对多)。一个公司可以有多个登录用户。用户保存在 ASP.Net Identity 生成的 AspNetUsers 表中。

我的公司模型如下:

public class Company
{
    public int CompanyID { get; set; }

    // Link naar de Userid in Identity: AspNetUsers.Id
    [Display(Name = "Username")]
    public …
Run Code Online (Sandbox Code Playgroud)

c# linq asp.net-mvc asp.net-identity

3
推荐指数
1
解决办法
7888
查看次数

ASP.NET Core 3.1“‘用户’不包含‘FindFirstValue’的定义”

在尝试从此处实现答案时>如何在 ASP.NET Core 中获取当前登录的用户 Id 以及用户将我重定向到此处> https://github.com/dotnet/aspnetcore/issues/18348

var UserId = User.FindFirstValue(ClaimTypes.Name);

^ 这不起作用,并打印以下错误'User' does not contain a definition for 'FindFirstValue'

编辑:添加控制器片段

我的控制器的完整片段...

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using ProjectName.Processing;
using ProjectName.Repository;
using Newtonsoft.Json;

namespace ProjectName.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ClassNameController : ControllerBase
    {
        private readonly ILogger<ClassNameController> _logger;
        private OtherClassNameProcessing proc = new OtherClassNameProcessing();

        public ClassNameController(ILogger<ClassNameController> logger)
        {
            _logger = logger;
        }

        [HttpGet]
        [ProducesResponseType(typeof(List<2ndOtherClassNameRepository>), StatusCodes.Status200OK)]
        public IActionResult GetUserName()
        { …
Run Code Online (Sandbox Code Playgroud)

.net-core asp.net-core asp.net-core-identity .net-core-3.1

3
推荐指数
2
解决办法
7177
查看次数