Displaying name after login asp.net webmatrix - asp.net

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)!"
Looking at the html code, I see
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.
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.
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:
#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.
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..

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
#{
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)
Then in the html markup I changed #WebSecurity.CurrentUserName to #authenticatedUser so now it reads:
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.

Related

How to GET and parse data from website? Unity3D

I am new at this and I do not know how to get info from site and show it in UI. I have tried using plugins but do not know how to. I want to get the numbers showing on the site which are constantly updated to be shown in a UI field when refresh button is click. Can someone please help I am stuck over here.
The site is results site which revise the numbers on it daily bases. The numbers are changing and its a active server page.
As Vancete said, WWW could be used to solve this.
Create a Coroutine, load the data from the webpage and then do something with it.
IEnumerator GetDataFromWebpage (string url)
{
WWW webpage = new WWW(url);
while (!webpage.isDone) yield return false;
string content = webpage.text;
. . .
// Do something with <content>
}
You can take a look here:
https://docs.unity3d.com/ScriptReference/WWW.html
So you should create an WWW object with the desired URL, wait for the response, then load the object.text property. After all, do a substring of what you need and you are done!
Feel free to ask if you need some more help.

How to verify text in a css class with Selenium?

I am using Selenium to automate app creation test. The test includes filling out fields on a web page and clicking a submit button. Once that is done, a new page loads with an alert stating success or failure. The problem for me is, the alert is coded in a css class as follows:
<div class="alert-box notice">
Successfully created application
×
</div>
All I need to do is verify the text "Successfully created application" exists. I do not need to manipulate anything.
I'm not sure what language you are doing, but basically, you need to get the text in the containing div.
So, for example,
driver.findElement(By.cssSelector(".alert-box.notice")).getText()
After some discussion with my peers and much online research, here is the solution I found.
At first I tried capturing the text based on Nathan's suggestion, but decided to store the results as a string.
String happy = driver.findElement(By.className("alert-box notice")).getText();
assertEquals(true, happy.contains("Successfully"));
The problem with this is that I kept getting errors such as, "org.openqa.selenium.InvalidSelectorException: The given selector alert-box notice is either invalid or does not result in a WebElement. The following error occurred:
InvalidSelectorError: Compound class names not permitted"
Since I have no control over the class name, I went the xpath route as follows:
String happy = driver.findElement(By.xpath("html/body/div/div[2]/div/div/div/div[3]/div")).getText();
assertEquals(true, happy.contains("Successfully"));
This version runs correctly and I am able to verify the alert message.

VB.Net PayPal Integration Description

I am working with a historical PayPal system in vb.net. I am struggling to add individual item descriptions or names for the products that the user is paying for. It is using the NVPSetExpressCheckout and the data is meant to display on the PayPal website when the user is about to pay. Instead however I am getting constant issues which I assume must be due to syntax or just the way I am trying to do it.
Here is the current code which works:
Dim ppSet As New NvpSetExpressCheckout()
ppSet.Add(NvpSetExpressCheckout.Request._AMT, Decimal.Parse(litTotal.Text))
ppSet.Add(NvpSetExpressCheckout.Request.CURRENCYCODE, "GBP")
Dim basePath As String = Request.Url.AbsoluteUri.Replace(Request.Url.PathAndQuery, String.Empty) + Request.ApplicationPath
ppSet.Add(NvpSetExpressCheckout.Request._RETURNURL, basePath & "paypal.aspx")
ppSet.Add(NvpSetExpressCheckout.Request._CANCELURL, basePath & "cancel.aspx")
I have then tried to add the a description using many methods such as:
ppSet.Add(NvpSetExpressCheckout.Request.L_DESC0, "First Item")
I however am simply getting errors like these:
'L_DESC0' is not a member of 'Encore.PayPal.Nvp.NvpSetExpressCheckout.Request'.
This issue is driving me mad and I can not find a fix. All documentation including the XML says that this is the correct way. I did try to just use the DESC field which worked however all the items just got displayed as one paragraph instead of being on separate lines. Help extremely appreciated.
Found the solution. It was due to the L_AMTn request not being submitted which is basically the item amounts. Once this adds up to the total cost it submits perfectly.

.NET frameworks for formatting e-mail messages?

Are there any open source/free frameworks available that take some of the pain out of building HTML e-mails in C#?
I maintain a number of standalone ASP.NET web forms whose main function is to send an e-mail. Most of these are in plain text format right now, because doing a nice HTML presentation is just too tedious.
I'd also be interested in other approaches to tackling this same problem.
EDIT: To be clear, I'm interested in taking plain text form input (name, address, phone number) and dropping it into an HTML e-mail template. That way the receipient would see a nicely formatted message instead of the primitive text output we're currently giving them.
EDIT 2: As I'm thinking more about this and about the answers the question has generated so far, I'm getting a clearer picture of what I'm looking for. Ideally I'd like a new class that would allow me to go:
HtmlMessage body = new HtmlMessage();
body.Header(imageLink);
body.Title("Some Text That Will Display as a Header");
body.Rows.Add("First Name", FirstName.Text);
The HtmlMessage class builds out a table, drops the images in place and adds new rows for each field that I add. It doesn't seem like it would be that hard to write, so if there's nothing out there, maybe I'll go that route
Andrew Davey created Postal which lets you do templated emails using any of the ASP.NET MVC view engines. Here's a video where he talks about how to use it.
His examples:
public class HomeController : Controller {
public ActionResult Index() {
dynamic email = new Email("Example");
email.To = "webninja#example.com";
email.FunnyLink = DB.GetRandomLolcatLink();
email.Send();
return View();
}
}
And the template using Razor:
To: #ViewBag.To From: lolcats#website.com Subject: Important Message
Hello, You wanted important web links right? Check out this:
#ViewBag.FunnyLink
<3
The C# port of StringTemplate worked well for me. I highly recommend it. The template file can have a number of named tokens like this:
...
<b>
Your information to login is as follows:<br />
Username: $username$<br />
Password: $password$<br />
</b>
...
...and you can load this template and populate it like this:
notificationTemplate.SetAttribute("username", Username);
notificationTemplate.SetAttribute("password", Password);
At the end, you get the ToString() of the template and assign it to the MailMessage.Body property.
I recently implemented what you're describing using MarkDownSharp. It was pretty much painless.
It's the same framework (minus a few tweaks) that StackOverflow uses to take plain-text-formatted posts and make them look like nice HTML.
Another option would be to use something like TinyMCE to give your users a WYWIWYG HTML editor. This would give them more power over the look and feel of their emails, but it might just overcomplicate things.
Bear in mind that there are also some security issues with user-generated HTML. Regardless of which strategy you use, you need to make sure you sanitize the user's input so they can't include scary things like script tags in their input.
Edit
Sorry, I didn't realize you were looking for an email templating solution. The simplest solution I've come up with is to enable text "macros" in user-generated content emails. So, for example, the user could input:
Dear {RecipientFirstName},
Thank you for your interest in {ClientCompanyName}. The position you applied for has the following minimum requirements:
- B.S. or greater in Computer Science or related field
- ...
And then we'd do some simple parsing to break this down to:
Dear {0},
Thank you for your interest in {1}. The position you applied for has the following minimum requirements:
- B.S. or greater in Computer Science or related field
- ...
... and ...
0 = "RecipientFirstName"
1 = "ClientCompanyName"
...
We store these two components in our database, and whenever we're ready to create a new instance from this template, we evaluate the values of the given property names, and use a standard format string call to generate the actual content.
string.Format(s, macroCodes.Select(c => EvaluateMacroCode(c, obj)).ToArray());
Then I use MarkdownSharp, along with some HTML sanitizing methods, to produce a nicely-formatted HTML email message:
Dear John,
Thank you for your interest in Microsoft. The position you applied for has the following minimum requirements:
B.S. or greater in Computer Science or related field
...
I'd be curious to know if there's something better out there, but I haven't found anything yet.

change user_profile_form form fields order

When a user login , the user will be redirect to a user profile page, which has a My account field set.
the field set has 2 fields, "Username: ", "Email address:". those 2 fields are generated by drupal.
those 2 field contained in a form which has a id ("user_profile_form") . I want to change the order of those 2 fields.
I have tried to intercept 'user_profile_form' , inside hook_form_alter.
code as follow:
$form['account']['name']['#weight'] = 1;
but that did not success, drupal did not even rendering the 'name' field, so no username: showed on browser.
What you did is absolutely correct, and probably did work. You can change the weight of the fields with the method described above.
The username field is not always rendered. The reason is that a persmission is required: change own username. If that perm is not set, you wont be allowed to alter you username and the field wont be shown.
Info on debugging.
Your info alone is not quite enough to debug. From what you describe, you are doing the right thing, but other modules could be making things a bit tricky for you. The devel module is quite good when it comes to debugging, ti defines two functions I use a lot when debugging:
dpm() pretty prints the variable to the message area using krumo.
dd() Prints / saves a variable to a log file. Useful when you can't view messages on the screen.
I would suggest that you look at the $form variable before and after you alter it.
Things that could make it go wrong:
Did you remember to pass the $form variable by reference using the & notation?
Is another module altering your form after you?
Are you checking for the correct form id, so you alter the correct form?
These are some pointers, before you bring more info, all I can do is guess to what your problem exactly can be. I did something like this a few days ago so I know what you describe shouldn't be a problem.

Resources