Click here to Skip to main content
15,886,002 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Everyone,

I am new to Silverlight. I want to implement windows authentication with active directory for silverlight application,

I have not found complete downloadable example. Can you please send me the links, where can i download the example?.

Can you suggest me, how can i achieve this functionality.

Thanks in Advance.
Posted
Updated 21-Jul-14 3:25am
v2

There is only one answer to your question... How to: Use Windows Authentication to Secure a Service for Silverlight Applications[^]

We cannot do the task for you, but reading the article you will find the path.
 
Share this answer
 
Hello Christian,

Thank you very much for your reply.

I've solved the issue myself.

I am posting the code below, which may help others:

Step 1:
Create new project and Create New Webservice.
Step 2:
Write the following method:

C#
[WebMethod(Description = "Validate a username and password against Active Directory")]
       public bool Authentication(string domain, string username, string password)
       {
           var val = true;
           try
           {
               DirectoryEntry entry = new DirectoryEntry("LDAP://" + domain, username, password);
               DirectorySearcher search = new DirectorySearcher(entry);

               search.Filter = "(SAMAccountName=" + username + ")";
               var uname = search.PropertiesToLoad.Add("cn");

               SearchResult result = search.FindOne();
               if (result == null)
               {
                   val = false;
               }
           }
           catch
           {
               val = false;
           }
           return val;
       }


Step 3:
Consume this web service in your project.

C#
private void Register_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                //User Name can not be blank
                if (txtUsername.Text.Trim() == "")
                {
                    lblStatus.Text = "Please Enter Username";
                }
                
                //Password can not be blank
                else if (txtPassword.Password == "")
                {
                    lblStatus.Text = "Please Enter Password";
                }

                else
                {
                    //Creating object for "your created web service name (I am taking my web service as Domain_Auth" web service for Authentication
// I am taking WS as proxy name here and 


                    WS.Domain_AuthSoapClient client = new WS.Domain_AuthSoapClient();
                    client.AuthenticationCompleted += new EventHandler<authenticationcompletedeventargs>(Client_AuthenticationCompleted);
                    client.AuthenticationAsync(" your domain name", txtUsername.Text, txtPassword.Password);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        //Event Handler for Authentication Method
        void Client_AuthenticationCompleted(object sender, AuthenticationCompletedEventArgs e)
        {

            if (e.Error != null)
            {
                lblStatus.Text = "Sorry, an error occured. " + e.Error.Message;
            }

            if (e.Result == true)
            {
                //Do something
            }
            else
            {
                //Do something
            }
        }


</authenticationcompletedeventargs>


Thank you very much.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900