C# – ASP.NET MVC + Silverlight + Forms Authentication

asp.net-mvccforms-authenticationsilverlight

So i am trying to get a simple system working where i have an asp.net mvc web app with the forms authentication already up and running with a user created. i can login with no problem using the mvc controller/view.

I then added a silverlight app to the solution, using the existing web app as the host. I created a silverlight-enabled web service and added an operation contract with the following code:

    [OperationContract]
    public bool Authenticate(string username, string password)
    {
        if (FormsAuthentication.Authenticate(username, password))
        {
            FormsAuthentication.SetAuthCookie(username, false);
            return true;
        }
        return false;
    }

In the silverlight app, i added two text boxes and a button, and a service reference to the WCF service. In the button click event, i have this code:

    void login_Click(object sender, RoutedEventArgs e)
    {
        AuthenticationService.AuthenticationClient client = new AuthenticationClient();
        client.AuthenticateCompleted += new EventHandler<AuthenticateCompletedEventArgs>(client_AuthenticateCompleted);
        client.AuthenticateAsync(username.Text, password.Text);
    }

    void client_AuthenticateCompleted(object sender, AuthenticateCompletedEventArgs e)
    {
        if (e.Result)
        {
            MessageBox.Show("Success");

        }
        else
        {
            MessageBox.Show("Error");
        }
    }

So the problem is, when i enter my login info and click the button, all i get is the error box. I can't seem to get it to authenticate the user.

What am i missing?

UPDATE:
Here is the error i get in the async complete handler:

Line: 86
Error: Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.NullReferenceException: Object reference not set to an instance of an object.
at UserPortal.MainPage.client_AuthenticateCompleted(Object sender, AuthenticateCompletedEventArgs e)
at UserPortal.AuthenticationService.AuthenticationClient.OnAuthenticateCompleted(Object state)

UPDATE 2:
So the error i posted above is because the e.Error property is null. So i am not getting any specific error from the authentication service. Is there something i need to change in the web.config to get this to work via silverlight?

    <authentication mode="Forms">
  <!-- forms loginUrl="~/Account/LogOn" timeout="2880"/ -->
    </authentication>
    <membership>
        <providers>
            <clear/>
            <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" passwordStrengthRegularExpression="" applicationName="/"/>
        </providers>
    </membership>
    <profile>
        <providers>
            <clear/>
            <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="ApplicationServices" applicationName="/"/>
        </providers>
    </profile>
    <roleManager enabled="false">
        <providers>
            <clear/>
            <add connectionStringName="ApplicationServices" applicationName="/" name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            <add applicationName="/" name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
        </providers>
    </roleManager>

Best Answer

Ok, so i got it to work, kinda.

Following the content here i managed to get a service up and running that would allow me to successfully login. The problem is i had to change the RequireSSL to false. I could not get the service to work running on https.

Anyone know what i need to do to get it to work on SSL? i am using the ASP.NET development server right now, do i need to configure a real version of IIS on this box for that to work?