You need your D number to open a bank account, to get a regular GP (fastlege), to rent a place to live and to pay your taxes.
If you have applied for protection (asylum), you will get a D number when you come to Norway. You will get it from the UDI or from the police (Politiets Utlendingsenhet) when you register.
If you have another type of residence permit and you will be living in Norway for less than six months, you will also get a D number. The Norwegian Tax Administration (Skatteetaten) has an overview of who you may contact to get a D number, depending on what you need it for in your situation. (external website)
You may then apply for a national identity number and use that instead of your D number.
A DUF number is not the same as a D number. The DUF number is your registration number in the computer system of the Norwegian Directorate of Immigration.