fm_strategy
fm_strategy

Reputation: 201

Authentication Webservice in Silverlight 4

I'm newbie to webservices.. I have a Silverlight 4 application, with a login page created be me. I have created a webservice:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;

namespace MagNET.Web.WebServices
{
    /// <summary>
    /// Summary description for AuthenticationService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class AuthenticationService : System.Web.Services.WebService
    {

        [WebMethod(EnableSession = true)]
        public bool AuthenticateUser(string username, string password)
        {
            //bool valid = Membership.ValidateUser(username, password);
            bool valid = true;

            if (valid)
            {
                FormsAuthentication.SetAuthCookie(username, false);
                Session["username"] = username;
            }
            else
            {
                Session["username"] = null;
            }

            return valid;
        }

        [WebMethod(EnableSession = true)]
        public string HelloWorld()
        {
            if (!IsLoggedIn())
            {
                throw new Exception("User is not logged in!!!");
            }
            else
            {
                return "Hello World!";
            }
        }

        private bool IsLoggedIn()
        {
            if (Session["username"] != null)
                return true;
            else
                return false;
        }

    }
}

I use this code to find out if user is logged in:

MagNET.AuthenticationService.AuthenticationServiceSoapClient svc =
     new MagNET.AuthenticationService.AuthenticationServiceSoapClient();
svc.HelloWorldCompleted += new EventHandler<AuthenticationService.HelloWorldCompletedEventArgs>(svc_HelloWorldCompleted);
svc.HelloWorldAsync();


void svc_HelloWorldCompleted(object sender, AuthenticationService.HelloWorldCompletedEventArgs e)
{
    MessageBox.Show("User logged in");
}

I don't know how to invoke the AuthenticateUser method of the web service (I don't know how to effectively log in a user when login button from my login page is pressed).

Upvotes: 1

Views: 783

Answers (1)

fm_strategy
fm_strategy

Reputation: 201

Just use AuthenticateUser method instead HelloWorld method:

MagNET.AuthenticationService.AuthenticationServiceSoapClient svc =
         new MagNET.AuthenticationService.AuthenticationServiceSoapClient();
    svc.AuthenticateUserCompleted += new EventHandler<AuthenticationService.AuthenticateUserCompletedEventArgs>(svc_AuthenticateUserCompleted);
    string username = txtBoxUsername.Text;
    string password = txtBoxPassword.Password;
    svc.AuthenticateUserAsync(username, password);

Upvotes: 1

Related Questions