tags:

views:

144

answers:

6

What would be the best way to properly concatenate string with an url inside? Can it be okay with this?

String.Format("This is the link: <a href={0}>{1}</a>", somevalue1, somevalue2)

Or it probably leaves open door for injection attacks?

So how would I insert a hyperlink within a string?

I guess I could do with HyperLink class, but then is there any property that returns html code for the class object?

A: 

You missed the quotes:

String.Format("This is the link: <a href=\"{0}\">{1}</a>", somevalue1, somevalue2)
User
Yes but it still won't encode it properly or securely
colithium
+1  A: 

This article talks about how to accomplish this in a secure way using HttpUtility.UrlEncode.

colithium
I don't believe that's what he's looking for. He's setting the `href` attribute and the text of the anchor. He's not building a URL that needs to have its query string values encoded.
Blixt
He asked about possible security concerns so I assumed he was taking input from users
colithium
A: 

You could override the .ToString() method of the class which would return the HTML i.e.

public class Hyperlink
{
    private string url;

    public Hyperlink(string URL)
    {
        this.url = URL;
    }

    public string DisplayText { get; set; }
    public string URL { get { return this.url; } } 

    public override string ToString()
    {
        return String.Format(@"<a href=""{0}"">{1}</a>", this.url, this.DisplayText);
    }

}

Then you could do something like:

var hl = new Hyperlink("www.stackoverflow.com");
hl.DisplayText = "Stack Overflow";
var link = String.Format("This is a link: {0}", hl);

Then you can apply whichever encoding you wish to the full string if need be.

James
Not URL Encoded
colithium
The question only asked how to put a HTML hyperlink into a string.
James
A: 

If you set what you get from your client to the link'!s href, it always causes a backdoor for injection attacks.

You should use HttpUtility.UrlEncode to encode querystrings of your links, to get secure links.

Canavar
+3  A: 

To avoid HTML injection, you would do this:

String.Format("This is the link: <a href=\"{0}\">{1}</a>",
    HttpUtility.HtmlEncode(somevalue1), HttpUtility.HtmlEncode(somevalue2))

Do not use UrlEncode for HTML values (attributes and text), because it will just look garbled. UrlEncode is for query string values, i.e. page.aspx?param=value+that+has+been+encoded

Blixt
+3  A: 

The below uses HyperLink and should work. However, consider whether you really need an intermediate string in the first place.

using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

HyperLink link = new HyperLink(){NavigateUrl="http://stackoverflow.com", Text = "StackOverflow"};
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
link.RenderControl(htw);
sw.Close();
String rendered = sw.ToString();
Matthew Flaschen
+1 for explaining how to get the HTML from the `HyperLink` control.
Blixt
Thank you. That's what I wanted to know :)
Janis Veinbergs