扩展ASP.NET身份 [英] Extending ASP.NET Identity

查看:146
本文介绍了扩展ASP.NET身份的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

看来这已经被问了很多次,在许多方面,其中没有一个似乎适合我确切的情况。

It seems this has been asked many times, in many ways, none of which seem to fit my exact situation.

下面是从我的_LoginPartial.cshtml文件中的一行:

Here's a line from my _LoginPartial.cshtml file:

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })

请参阅上面写着User.Identity.GetUserName部分()?

See the part that says User.Identity.GetUserName()?

我想将它更改为User.Identity.FirstName或User.Identity.GetFirstName()。

I want to change it to User.Identity.FirstName or User.Identity.GetFirstName().

我不希望它说你好电子邮件地址,而是您好鲍勃

I don't want it to say "Hello email address", but rather "Hello Bob"

我的想法是,我只是想显示在Identity类一个新的属性(或方法)。显然,它必须比这更多。

My thought is that I'm simply wanting to show a new property (or method) on the Identity class. Obviously it must be more than that.

我已经添加了名字属性和它是可用中的AccountController。

I've added the FirstName property and it is available in the AccountController.

public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

它不会暴露在_LoginPartial文件。我希望它暴露在那里!

It does not get exposed in the _LoginPartial file. I want it exposed there!

感谢您的帮助。

推荐答案

即使你的答案是不是正是我想要的东西,您的意见促使我此解决方案:

Even though your answer wasn't "exactly" what I wanted, your comments led me to this solution:

@using Microsoft.AspNet.Identity
@using YourModelnameHere.Models
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @{
                var manager = new UserManager<ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(User.Identity.GetUserId()); 
            }
            @Html.ActionLink("Hello " + currentUser.FirstName + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })

            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" }) // I no longer need this ActionLink!!!
        </li>
    </ul>
    }
}

在我IdentityModel.cs文件,我添加了两个属性名字和姓氏

And in my IdentityModel.cs file, I added the two properties FirstName and LastName

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

这篇关于扩展ASP.NET身份的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆