True, it is outside the diatonic notes - let's face it, F♯ isn't one of those! So, it's not strictly within that key.
However, since the other two notes are, it gets close. It's actually (the whole chord) the V of V - the dominant of the dominant, so pointing towards the dominant of the original key.
It's a secondary dominant, and as such, leads the music on to another diatonic chord - the dominant itself.
EDIT: - with reflection, the question itself is asked in a naiive way (with respect). It asks two questions - whether the chord D is 'within the scale' (header), then at the end whether it's 'within the key'. The two are not interchangable. 1st answer - no, it's not within the scale - it cannot be - there's F# included. So it's not diatonic.
2nd answer - within the key - certainly, as any key will use, or has the propensity to use - at least the secondary dominants that are inextricably associated with that key. Thus - sec. doms which could actually move diectly to a diatonic chord (but don't necessarily have to) are:E(7), B(7), A(7), D(7), even C7, none of which are diatonic, all of which are 'within the key'.
As stated in gui3's comment, F# does get found in a parallel mode - that of C Lydian. It's fairly common to use parallel modes or keys, which will still use the original root (here, C), but change certain other notes (here, F>F#). Thus, in C Lydian, the triad built on root D will be D, F#, A - D major.