Wednesday, May 27, 2009

Windows authentication in Silverlight

Authenticating a windows user from a Silverlight application is pretty simple with the following steps

1.Create a WCF service with a method to Authenticate the windows user

Interface

[ServiceContract]
public interface IUser
{
[OperationContract]
bool AuthenticateUser(string UserName, string Password);
}



Class




public class User : IUser
{
public bool AuthenticateUser(string UserName, string Password)
{
return (Helper.LogOnUser(UserName.Split(new string[] { "@" }, StringSplitOptions.RemoveEmptyEntries)[0],
"microsoft.com",
Password
) != 0);
}

}



Helper class




public static class Helper
{
#region Properties
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_PROVIDER_DEFAULT = 0;
#endregion

#region Static

#region NativeMethods
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
private static extern int LogonUser(String UserName,
String Domain,
String Password,
int LogonType,
int LogonProvider,
ref IntPtr Token);

#endregion

#region Methods
public static int LogOnUser(String userName, String domain, String password)
{
IntPtr token = IntPtr.Zero;
return LogonUser(userName, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token);
}
#endregion
#endregion
}



2.Create the service proxy from the Silverlight application and call the Authentication method described in the WCF service



Silverlight Code behind




UserService.UserClient Proxy = new UserService.UserClient();
Proxy.AuthenticateUserCompleted +=
new EventHandler<UserService.AuthenticateUserCompletedEventArgs>(Proxy_AuthenticateUserCompleted);
Proxy.AuthenticateUserAsync(txtUser.Text, txtPassword.Password);




void Proxy_AuthenticateUserCompleted(object sender, UserService.AuthenticateUserCompletedEventArgs e)
{
MessageBox.Show(e.Result.Equals(true) ? "login succeeded" : "login failed");
}

Wednesday, May 20, 2009

Error 500.19 – Internal Server Error - While trying you run a WCF Service hosted on a Vista Machine

When you are deploying a WCF service on a newly set up Vista machine chances are that you get the following error






One possible option to get the service working is by resetting the registration of ServiceModel on your machine.


The ServiceModel Registration Tool will help you in accomplishing this task.


The tool can be found in the following location:


%SystemRoot%\Microsoft.Net\Framework\v3.0\Windows Communication Foundation\


Open Visual studio Command prompt and run the following command : ServiceModelReg.exe /i



Other helpful links




http://support.microsoft.com/kb/942055