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");
}
No comments:
Post a Comment