登录asp.net webmatrix后显示名称

时间:2022-07-28 16:49:08

I built a site using the starter site template in Webmatrix that uses a log in. After a user logs in, it displays near the top "Hello, (email address)!"

我使用Webmatrix中的初始站点模板构建了一个使用登录的站点。用户登录后,它显示在“Hello,(电子邮件地址)!”顶部附近。

Looking at the html code, I see

看一下html代码,我明白了

Hello, <a id="logname" class="email" href="~/Account/Manage" title="Manage">@WebSecurity.CurrentUserName</a>!

I'm just a beginner with asp.net but can work things out following logic, so what I have done is added a 'Name' field to the database where the email and password is stored and edited the registration page so users can enter their name as well, it writes to the database no problems.

我只是asp.net的初学者,但可以按照逻辑进行操作,所以我所做的是在数据库中添加一个'Name'字段,用于存储电子邮件和密码并编辑注册页面,以便用户输入他们的名称也是如此,它写入数据库没有问题。

Now I assumed it's just a matter of finding where @WebSecurity.CurrentUserName is referring to the user's email address and make it point to the Name field instead. However I've searched high and low and can not seem to find it anywhere.

现在我假设这只是找到@WebSecurity.CurrentUserName引用用户的电子邮件地址并将其指向Name字段的问题。然而,我搜索高低,似乎无法在任何地方找到它。

So my next approach was to use code from a Bakery database tutorial that fetches data from a field and modify it to grab the name and display it where I want, the code I tried to use here:

所以我的下一个方法是使用来自Bakery数据库教程的代码,该教程从字段中获取数据并修改它以获取名称并将其显示在我想要的位置,我试图在这里使用的代码:

@foreach(var row in db.Query(selectQueryString))

This appeared to work at first, It displayed "Hello, Ben!" but then if I made any more accounts it started to display them too, so after a while I was getting "Hello, Ben, Steve, Grace, etc..."

这似乎首先起作用,显示“你好,本!”但是如果我再做了一些账户,它也会开始显示它们,所以过了一会儿我就得到了“你好,本,史蒂夫,格蕾丝等......”

Even though I'm a beginner I do realize that this code isn't quite what I need, but something similar that takes that single Name field but only from the currently logged in user... I've googled and searched and seen many people wanting the same thing, except it's using PHP or VB or something that isn't Webmatrix.

尽管我是初学者,但我确实意识到这段代码并不是我所需要的,但类似的东西需要单一的Name字段,但只能从当前登录的用户...我用Google搜索并搜索并看到很多人们想要相同的东西,除了它使用PHP或VB或不是Webmatrix的东西。

If it's a quick answer I'd appreciate the code I need, or otherwise pointed in the right direction, any help would be greatly appreciated. Thanks in advance..

如果这是一个快速的答案,我会欣赏我需要的代码,或者指向正确的方向,任何帮助将不胜感激。提前致谢..

1 个解决方案

#1


0  

OK ironically I find the solution moments after posting the question, so here goes..

讽刺的是,我在发布问题后找到解决方案的时刻,所以这里......

This is the code needed to be put at the very top of the file _SiteLayout.chtml

这是需要放在文件_SiteLayout.chtml顶部的代码

@{
var authenticatedUser = "";

if (WebSecurity.IsAuthenticated) {
authenticatedUser = WebSecurity.CurrentUserName;
var db = Database.Open("StarterSite");
var UserData = db.QuerySingle("SELECT Name FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", authenticatedUser);
authenticatedUser = UserData.Name;
    }
}

StarterSite is the name of the database, and UserProfile is the name of the Table, and I labeled the name field Name (explained for the purpose of beginners like me who might be reading this)

StarterSite是数据库的名称,UserProfile是Table的名称,我将名称字段标记为Name(为了像我这样可能正在阅读此内容的初学者的目的进行了解释)

Then in the html markup I changed @WebSecurity.CurrentUserName to @authenticatedUser so now it reads:

然后在html标记中我将@ WebSecurity.CurrentUserName更改为@authenticatedUser,现在它显示为:

Hello, <a id="logname" class="email" href="~/Account/Manage" title="Manage">@authenticatedUser</a>!

resulting in the desired outcome of it now displaying the user's name instead of their email address.

导致它的所需结果现在显示用户的名字而不是他们的电子邮件地址。

#1


0  

OK ironically I find the solution moments after posting the question, so here goes..

讽刺的是,我在发布问题后找到解决方案的时刻,所以这里......

This is the code needed to be put at the very top of the file _SiteLayout.chtml

这是需要放在文件_SiteLayout.chtml顶部的代码

@{
var authenticatedUser = "";

if (WebSecurity.IsAuthenticated) {
authenticatedUser = WebSecurity.CurrentUserName;
var db = Database.Open("StarterSite");
var UserData = db.QuerySingle("SELECT Name FROM UserProfile WHERE LOWER(Email) = LOWER(@0)", authenticatedUser);
authenticatedUser = UserData.Name;
    }
}

StarterSite is the name of the database, and UserProfile is the name of the Table, and I labeled the name field Name (explained for the purpose of beginners like me who might be reading this)

StarterSite是数据库的名称,UserProfile是Table的名称,我将名称字段标记为Name(为了像我这样可能正在阅读此内容的初学者的目的进行了解释)

Then in the html markup I changed @WebSecurity.CurrentUserName to @authenticatedUser so now it reads:

然后在html标记中我将@ WebSecurity.CurrentUserName更改为@authenticatedUser,现在它显示为:

Hello, <a id="logname" class="email" href="~/Account/Manage" title="Manage">@authenticatedUser</a>!

resulting in the desired outcome of it now displaying the user's name instead of their email address.

导致它的所需结果现在显示用户的名字而不是他们的电子邮件地址。