IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Framework .NET Discussion :

PasswordBox WPF et Silverlight


Sujet :

Framework .NET

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre Expert Avatar de meziantou
    Homme Profil pro
    autre
    Inscrit en
    Avril 2010
    Messages
    1 223
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : autre
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 1 223
    Par défaut PasswordBox WPF et Silverlight
    Bonjour,

    En WPF il est impossible de binder la propriété Password d'une PasswordBox. La raison invoquée est la sécurité.
    Cependant en silverlight (depuis la version 3) c'est possible.

    Quelqu'un connait-il la raison de cette différence ?

  2. #2
    Rédacteur/Modérateur


    Homme Profil pro
    Développeur .NET
    Inscrit en
    Février 2004
    Messages
    19 875
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 43
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur .NET
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Février 2004
    Messages : 19 875
    Par défaut
    En WPF le mot de passe n'est pas stocké en clair en mémoire, il est stocké dans une SecureString (accessible via la propriété SecurePassword). La propriété Password matérialise le mot de passe en clair.

    Pour pouvoir binder la propriété Password, il faudrait que ce soit une DependencyProperty, et dans ce cas sa valeur serait stockée en mémoire ce qui annule l'intérêt du SecurePassword.

    En Silverlight, la classe SecureString n'est pas disponible, donc pas de SecurePassword, donc le mot de passe est stocké en clair en mémoire de toutes façons. Donc il n'y a pas de raison que ce ne soit pas une DependencyProperty...

    Perso j'ai jamais été très convaincu de l'intérêt de la classe SecureString. A un moment ou un autre, on va avoir besoin du password en clair de toutes façons...

    Il y a une astuce pour contourner ce problème : il faut créer une propriété attachée qui va gérer le password via l'évènement PasswordChanged.

    Code C# : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
        public static class PasswordBoxBehavior
        {
            public static bool GetBindPassword(DependencyObject obj)
            {
                return (bool)obj.GetValue(BindPasswordProperty);
            }
     
            public static void SetBindPassword(DependencyObject obj, bool value)
            {
                obj.SetValue(BindPasswordProperty, value);
            }
     
            public static readonly DependencyProperty BindPasswordProperty =
                DependencyProperty.RegisterAttached(
                  "BindPassword",
                  typeof(bool),
                  typeof(PasswordBoxBehavior),
                  new UIPropertyMetadata(
                    false,
                    BindPasswordChanged));
     
            private static void BindPasswordChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                var passwordBox = o as PasswordBox;
                if (passwordBox == null)
                    return;
     
                var oldValue = (bool)e.OldValue;
                var newValue = (bool)e.NewValue;
     
                if (oldValue && !newValue)
                {
                    passwordBox.PasswordChanged -= passwordBox_PasswordChanged;
                }
                else if (newValue && !oldValue)
                {
                    string password = GetPassword(passwordBox);
                    if (!string.IsNullOrEmpty(password))
                        passwordBox.Password = password;
                    passwordBox.PasswordChanged += passwordBox_PasswordChanged;
                }
            }
     
            static void passwordBox_PasswordChanged(object sender, RoutedEventArgs e)
            {
                var passwordBox = (PasswordBox)sender;
                if (passwordBox == null)
                    return;
     
                SetPassword(passwordBox, passwordBox.Password);
            }
     
            public static string GetPassword(DependencyObject obj)
            {
                return (string)obj.GetValue(PasswordProperty);
            }
     
            public static void SetPassword(DependencyObject obj, string value)
            {
                obj.SetValue(PasswordProperty, value);
            }
     
            public static readonly DependencyProperty PasswordProperty =
                DependencyProperty.RegisterAttached(
                    "Password",
                    typeof(string),
                    typeof(PasswordBoxBehavior),
                    new FrameworkPropertyMetadata(
                        null,
                        FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                        PasswordChanged));
     
            private static void PasswordChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
            {
                var passwordBox = o as PasswordBox;
                if (passwordBox == null)
                    return;
     
                string newValue = (string)e.NewValue;
     
                if (GetBindPassword(passwordBox))
                {
                    string current = passwordBox.Password;
                    if (current != newValue)
                        passwordBox.Password = newValue;
                }
            }
        }

    Ca s'utilise comme ça :

    Code XAML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    <PasswordBox Name="pwd"
                 my:PasswordBoxBehavior.BindPassword="True"
                 my:PasswordBoxBehavior.Password="{Binding Password, UpdateSourceTrigger=PropertyChanged}" />

  3. #3
    Membre Expert Avatar de meziantou
    Homme Profil pro
    autre
    Inscrit en
    Avril 2010
    Messages
    1 223
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Autre

    Informations professionnelles :
    Activité : autre
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Avril 2010
    Messages : 1 223
    Par défaut
    Merci pour cette précision.
    Je trouve quand même très bizarre que dans un cas on considère la sécurité et que dans l'autre on n'en ai plus rien à faire...

    Perso j'ai jamais été très convaincu de l'intérêt de la classe SecureString. A un moment ou un autre, on va avoir besoin du password en clair de toutes façons...
    Je suis d'accord avec toi

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Interopérabilitée entre WPF et Silverlight
    Par Archeone dans le forum Windows Presentation Foundation
    Réponses: 9
    Dernier message: 26/07/2010, 10h47
  2. Incomptabilité WPF et Silverlight
    Par cfeltz dans le forum Windows Presentation Foundation
    Réponses: 6
    Dernier message: 13/05/2009, 21h50
  3. Comment choisir entre WPF et Silverlight ?
    Par blepeign dans le forum Windows Presentation Foundation
    Réponses: 10
    Dernier message: 30/04/2009, 15h35
  4. WCF avec WPF ET Silverlight
    Par silberfab dans le forum Windows Communication Foundation
    Réponses: 0
    Dernier message: 14/04/2009, 08h50
  5. wpf / wpf browser / silverlight
    Par julien_iz dans le forum Windows Presentation Foundation
    Réponses: 8
    Dernier message: 12/03/2009, 18h24

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo