【问题标题】:Display First Name instead of UserName in welcome greeting of VB Web Form App在 VB Web Form App 的欢迎问候语中显示名字而不是用户名
【发布时间】:2019-06-08 12:57:08
【问题描述】:

Visual Studio 2017 中的常用 Visual Basic Web 表单应用模板会显示一条欢迎消息,并显示用户名字段(即“Hello, abc@123.com”)。我需要使用 FirstName 字段来显示它(即“你好,John)。

还有很多其他关于这个的帖子,但没有一个能解决我的问题。我已将 FirstName 属性添加到 identitymodels.vb 文件中,如下所示:

Private FName As String
    Public Property FirstName() As String
        Get
            Return FName
        End Get
        Set(ByVal value As String)
            FName = value
        End Set
    End Property

但是,FirstName 属性似乎没有在 sitemaster.aspx 文件中公开。我也尝试过 FName,结果相同。

当前在 sitemaster.aspx 文件中,用户名显示为:

<li>
<a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetUserName()  %>!</a>
</li>

我需要类似的东西,

<li>
<a runat="server" href="~/Account/Manage" title="Manage your account">Hello, <%: Context.User.Identity.GetFirstName()  %>!</a>
</li>

但是“GetFirstName 部分不起作用。我假设 GetUserName 是一个预定义的方法,我需要能够预定义一个返回 FirstName 字段而不是 UserName 字段的新方法。

【问题讨论】:

    标签: asp.net vb.net webforms


    【解决方案1】:

    当用户登录时,您可以通过执行以下操作来显示个人资料信息:

    1. Site.Master后面的代码中添加一个函数来获取User对象:
    Protected Function Get_Current_User() As ApplicationUser
        Dim manager = New UserManager(Of ApplicationUser)(New UserStore(Of ApplicationUser)(New ApplicationDbContext()))
        Dim currentUser = manager.FindById(Context.User.Identity.GetUserId())
        Return currentUser
    End Function
    
    1. 在您的标记中调用它:
    <li>
        <a runat="server" href="~/Account/Manage" title="Manage your account">
            Hello, <%: Get_Current_User().FirstName  %>!
        </a>
    </li>
    

    您需要以下导入才能使上述代码正常工作:

    Imports Microsoft.AspNet.Identity
    Imports Microsoft.AspNet.Identity.EntityFramework
    

    作为参考,这里有一篇很好地描述它的文章(在 C# 中):

    Customizing profile information in ASP.NET Identity in VS templates

    【讨论】:

    • 好吧,我试过这个并阅读了这篇文章。但是,我收到以下错误:“未定义类型'UserStore'。我在 site.master.vb 文件的顶部有以下导入:'''Imports Microsoft.AspNet.Identity Imports Microsoft.Owin.Security Imports Microsoft.Owin.Host.SystemWeb''' 'UserStore' 不应该包含在 Owin 中吗?
    • @jamie-dixson 你需要这两个导入:Imports Microsoft.AspNet.IdentityImports Microsoft.AspNet.Identity.EntityFramework。我也更新了我的答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 2021-11-30
    • 1970-01-01
    • 2016-01-26
    相关资源
    最近更新 更多