How can I (programmatically) display a custom block with user profile links in dropdown? I would like to display a custom block with some links for authenticated users like drupal.org does. 

I would like to display username next to user's picture, and custom menu items. For example, "My contents" where url is something like this: "/user/{uid}/my-contents".

Do you have any idea how to create that?

Ps: I tried "menu_token" module but it didn't work for me correctly.
If you don't have any issue rendering a block of menu, you can just show up a menu there which I suppose you've got that covered. for the UID part there are couple of workarounds for this: 


Using Views 
Menu Token
Me (Drupal 7) only
Templating: Much easier approach


The Views way

Number 1, using views is a long story, I'll keep it short and I move on to number 2, my own approach. With views you can use contextual filters and build your own custom links using "Current logged in user" filter, you can randomly get a view from whatever and override it with the HTML structure of your need. in case you need a more in depth answer for this, let me know and I will update my answer

Menu Token

This module is exactly what we need, but we have to consider some bugs that it currently has, if you check the issue queue there are some major issues with it, but still, you can manage to get it working with a little bit of dirty hacking, using jQuery/JavaScript

The issue with this module is that you cannot use the token in middle of a string for instance: /user/{uid}/my-contents wouldn't work but simply /{uid} works just fine.

So in our case, we override the href attribute and hardcode the path we want to it as prefix and suffix, using the UID

This is not the best practice at all, but in case anyone wants a quick solution, this is what I suggest, in case you need help with the jQuery part, let me know and I will update the answer.

ME module - Drupal 7 only

for those who have this issue in Drupal 7, this module considers /me as the user ID if configured properly, so a link in question would become something like this: /user/me/my-contents

Templating

So let's say we edit our page.html.twig file so we can create our custom structure for our menu, then in our case, we can simply refer to the current user's UID like so:

&lt;a href="{{ path('entity.user.canonical', {'user': user.id}) }}/my-contents"&gt;Test menu item&lt;/a&gt;