简体   繁体   English

剃刀视图中的MVC6核心身份声明

[英]MVC6 CORE identity claims in razor view

I am fiddling around with identity and I am definitely struggling - I have searched and searched, but nothing shows. 我在摆弄身份,我肯定在挣扎-我已经搜索了很多,但没有任何显示。

I want to add claims to identity and then be able to post these in the view on my page. 我想添加身份声明,然后将其发布到页面上的视图中。

 public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        var authenticationType = "Basic";
        var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);

        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FirstName", this.FirstName));

        return userIdentity;
    }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Address { get; set; }

In my register user i save these with the user: 在我的注册用户中,我将这些保存到用户:

public async Task<IActionResult> Register(RegisterViewModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName};
            var result = await _userManager.CreateAsync(user, model.Password);
            if (result.Succeeded)
            {
                // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
                // Send an email with this link
                //var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
                //var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
                //await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
                //    "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
                await _signInManager.SignInAsync(user, isPersistent: false);
                _logger.LogInformation(3, "User created a new account with password.");
                return RedirectToAction(nameof(HomeController.Index), "Home");
            }
            AddErrors(result);
        }

        // If we got this far, something failed, redisplay form
        return View(model);
    }

Note, that i checked my database and FirstName and LastName are being stored! 请注意,我检查了我的数据库,并存储了FirstName和LastName!

I then extended my identity (Note i removed the original return, to check if it indeed returned null) 然后,我扩展了身份(注意,我删除了原始返回值,以检查它是否确实返回null)

 public static class IdentityExtension
{
    public static string GetFirstName(this IIdentity identity)
    {
        var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");
        // Test for null to avoid issues during local testing
        // return (claim != null) ? claim.Value : string.Empty;
        return claim.ToString() ;
    }


}

I now figured I should be able to access the data from my razor view, which is in _Navigation.cshtml 我现在认为我应该能够从_Navigation.cshtml中的剃刀视图访问数据。

@if (Context.User.Identity.IsAuthenticated)
            {
                <div class="dropdown profile-element">
                    <a data-toggle="dropdown" class="dropdown-toggle" href="#">
                        <span class="clear">
                            <span class="block m-t-xs">
                                <strong class="font-bold">Hello @User.Identity.GetFirstName()</strong>
                            </span>

I also attempted to do this without the extension, directly: 我也尝试不使用扩展名直接执行此操作:

 @{
 if (Context.User.Identity.IsAuthenticated)
 {
     var FirstName = ((ClaimsIdentity)User.Identity).FindFirst("FirstName");
 } 
}

I am obviously missing something.. I can access the regular username without a problem. 我显然缺少一些东西。我可以正常访问常规用户名。 I have searched and found a lot to even get here, but a lot of it is different versions, so im stuck.. 我搜索了很多东西,甚至找到了很多东西,但是很多都是不同的版本,所以我被卡住了。

I'd appreciate if you can help me out! 如果您能帮助我,我将不胜感激!

Thanks. 谢谢。

请遵循此处: 定制索赔转换

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 .net核心mvc模拟身份和声明,并测试用户是否有声明 - .net core mvc Mocking Identity and claims and test if user has claim MVC6 / EF7:播种主用户和声明 - MVC6/EF7: Seeding Master User and Claims ASP.NET Core 2.0/Razor Pages - 如何在请求之间将数据保存在 NonFactors MVC6 网格中? - ASP.NET Core 2.0/Razor Pages - How can I persist data in a NonFactors MVC6 grid between requests? 如何将IFormFile放入MVC6中的View模型中 - How to put an IFormFile into a View Model in MVC6 User.Identity.Name 在 PasswordSignInAsync MVC .Net Core 3.0 之后始终为 null 并声明计数为 0 - User.Identity.Name always null and claims count 0 after PasswordSignInAsync MVC .Net Core 3.0 在ASP.Net MVC6 Identity框架中实现继承 - Implement inheritence in ASP.Net MVC6 Identity framework 使用 MVC6 和新身份设置 UserRole 管理 - Setting up UserRole management with MVC6 and the new Identity MVC6中的ASP身份 - 登录路径属性不起作用 - ASP Identity in MVC6 - Login Path property not working 声明身份 .NET CORE 3.0 API JWT - Claims Identity .NET CORE 3.0 API JWT AspNet Identity Core - 登录时的自定义声明 - AspNet Identity Core - Custom Claims on Login
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM