0

When checking the user against active directory, I also check their mail attribute try to store it into a session variable

However, when the users AD account doesn't have a mail address, I would like to display this on the screen for them.

At the moment an exception is caught for index out of range

How can I can check for this so that I can set the label property? I obviously want to avoid setting it in the catch.

         try
            {
                var mail = adsSearchResult.Properties["mail"][0] ;
                if (mail == null)
                {
                    Label3.Text = "blablabla";
                    return;
                }
                else
                {
                    Session["email"] = adsSearchResult.Properties["mail"][0].ToString().ToLower();
                }
            }
            catch (Exception ex)
            { 
              Response.Write(ex); 
            }
4

1 に答える 1