CSLA .NET

Vibrant discussion about CSLA .NET and using the framework to build great business applications.

ASP.Net 2.0 Provider Model - Code

This post has 30 Replies | 4 Followers

Not Ranked
Posts 11
colema18 replied on Thu, Jan 17 2008 5:55 PM
Hey Chris,

Do you think you can post or email me your principal and identity classes you are using for this sample?  I am still having trouble and the provider/identity I am using is the code from the PTracker sample.

Thanks
~james
colema18@gmail.com
Top 10 Contributor
Posts 629

Hey James

I'm attaching the principal/identity that I had when I first put together this code and should be representative of an identity/principal that works with this approach.

If you are having specific difficulties, please elaborate - information on where the process is breaking down is useful in giving you information you might be able to leverage.

Good luck,

Chris

 

Not Ranked
Posts 11
colema18 replied on Fri, Jan 18 2008 9:40 AM
Thanks for the quick response Chris.  I am indeed having the same issue so it doesn't look like it is related to the principal or identity.  I didn't give specifics because I am actually trying to get CSLA 3.5, ASP.MVC (.NET 3.5 extensions) and the Membership Provider all working together.

If you have the time and desire (and visual studio 2008) you can take a look at my source code here: http://www.agileblue.net/CSLAProviderSpike.zip

I have tried it with a regualr asp.net website instead of a asp.mvc website to make sure it didn't have to do with the MVC framework and I still get the same error.  It looks like when the Logon is getting called on the Principal object and it makes it's way to the WCF Data Portal it is mad because the principal is of type GenericPrincipal instead of BusinessPrincipal.

Is it a catch 22 scenario, where you need to call Logon to get a BusinessPrincipal set but you have to have a BusinessPrincipal to call Logon?

Exception: Principal must be of type BusinessPrincipal, not System.Security.Principal.GenericPrincipal

The Controller that Calls the Method:
        [ControllerAction]
        public void Authenticate(string username, string password)
        {
            if (ProPrincipal.LogOn(username, password))
            {
                System.Web.HttpContext.Current.Session["CslaPrincipal"] = Csla.ApplicationContext.User;
                System.Web.Security.FormsAuthentication.RedirectFromLoginPage(username, false);
            }
        }

Thanks for the help.
~james

Top 10 Contributor
Posts 629
skagen00 replied on Fri, Jan 18 2008 10:48 AM

I'm afraid I don't have a whole lot of time to look at your code right now. The message you have (the exception) is I'm pretty sure a common one that you might want to do a little searching on the forums for.

This one seemed to mention it in conjunction with WCF:

http://forums.lhotka.net/forums/post/14931.aspx

The way I was doing the authentication at the time of my posted code (it's gotten a little more robust for multiple client handling) but still works fine is in the login control's authenticate event (could be done wherever of course):

e.Authenticated = ProPrincipal.LogOn(Login1.UserName, Login1.Password);

HttpContext.Current.Session["CslaPrincipal"] = Csla.ApplicationContext.User;

That is, authenticate the user using the principal's logon, and then store the principal in a session variable for future requests - reestablishing it on future requests from the session variable.

I wish I had more time to assist you right now but I'm afraid I don't -- I can assure you that we haven't had any problems running with this code so as far as I'm concerned this is still entirely a viable approach and represents our approach. Do a little searching on the forums and see if that helps you out.

Also, I'm assuming running locally you don't have a problem but it's with remoting when it starts flaking out? There was a post on the forum where someone had the casing of CSLA wrong in the config file of the remoting server and that was causing the same exception you listed...

Good luck,

Chris

 

 

Top 100 Contributor
Posts 86
JoOfMetL replied on Fri, Feb 1 2008 11:30 AM

Hi I have the same problem as you. In my opinion the problem is that the AcquireRequestState in Global.asax is not called before the method in CslaRole(or Membership) is called (I am sur, i have checked with Debugger)
 
So By Default The type Csla.ApplicationContext.User is RolePrincipal and there is some error.

I Use the Csla 3.0.2 and I Have a WebSite which use a WebService.

I don't understand how it's working to the other people because Csla.ApplicationContext isn't correctly initialized.

I Solve (?????) the problem, i have put the code in the method ExecuteRemoteMethod (both CslaMembershipProvider, and CslaRoleProvider) :

PTPrincipal principal = Csla.ApplicationContext.User as PTPrincipal;
            if (principal == null)
                PTPrincipal.Logout();

So in the Csla.ApplicationContext.USer you have a object PTPrincipal and it is unantuthtificated, so the type is good and no error, but Not the Good PTPtincipal Object.
BUT, just after you pass by the global.asax and you put the good PTPtincipal with Session.

Why I don't put directlty the good PTPrincipal in session, because when CslaProvider is called, the Session is null
Top 100 Contributor
Posts 78
Aaron replied on Wed, Jun 25 2008 4:32 PM
Ok enough insanity! I've gone through this piece of code too many times. I continously get the same error of ArgumentNullException in the CSLARoleProvider Initialization method. The name and config parameters have proper values. Now this exception isn't being thrown at the "If config Is Nothing Then Throw ArgumentNullException("config"). It actually skips over this conditional statement as config being of something value. I believe the problem really happens during the conversion from functionalprovider variable Type to the RoleProvider Type. I dont know why? The functionalProvider variable before the conversion has of type value. Can anyone help me ?
The Keyboard Cowboy
Top 10 Contributor
Posts 629
skagen00 replied on Wed, Jun 25 2008 5:18 PM

Hey there -

Is it when you initialize the wrapped provider that you get the error? Which line exactly are you bombing out on?

If you're talking about when Activator.CreateInstance is run and then cast, try breaking it into two lines such as:

      object instance = Activator.CreateInstance(functionalProviderType); // Verify creation
      _wrappedProvider = (RoleProvider)instance;

Please give a bit more detail if this isn't where it's bombing out.

Chris

Top 100 Contributor
Posts 78
Aaron replied on Wed, Jun 25 2008 10:50 PM

ok that was the exact line that was bombing out. So i've taken your advice and seperated the Activator.CreateInstance from the conversion. That line is the exact line where it's bombing out. The instance has values of Name="Nothing", ApplicationName="Nothing" and Description="Nothing". I know I've supply these values in the web.config with their proper values. So why would it be bombing out at this point? How can this be corrected?

The Keyboard Cowboy
Top 10 Contributor
Posts 629
skagen00 replied on Wed, Jun 25 2008 11:04 PM

It's OK if the instance doesn't have values, because those will get supplied when this line is invoked:

            _wrappedProvider.Initialize(name, config);

It's still unclear to me which line was exactly bombing out. It created the instance of SqlRoleProvider OK (when breaking out the lines so that the only thing one line does is create an instance of SqlRoleProvider? Or did Activator.CreateInstance fail?

If Activator.CreateInstance failed, did you have a value for functionalProviderType that was established from Type.GetType()? 

It's a little tricky to diagnose w/ the information given so far, but I'm trying!

 

Top 100 Contributor
Posts 78
Aaron replied on Thu, Jun 26 2008 11:15 AM

Ok it bombs out right after the Initialize method and the next step that its going to is the Property Name, which has a return value of Nothing. So I'm guessing this whole problem is in the Initialize Method that isnt' fulfilling everything. This is my code, which isn't anything different from some of the samples or anything in this thread.

To answer your questions if the _functionProviderType has values, yes it does. It has the Name="SqlRoleProvider" and the FullName="System.Web.Security.SqlRoleProvider......". 

 

Public Class CustomRoleProvider

Inherits RoleProvider

Private _wrappedprovider As RoleProvider = Nothing

#Region "Properties"

Private Shared ReadOnly Property ExecutingOnServer() As Boolean

Get

Return Csla.ApplicationContext.DataPortalProxy = "Local" OrElse Csla.ApplicationContext.ExecutionLocation = ExecutionLocations.Server

End Get

End Property

Public Overrides ReadOnly Property Name() As String

Get

Return _wrappedprovider.Name      <----- Bombs out with a value of "Nothing"

End Get

End Property

Public Overrides ReadOnly Property Description() As String

Get

Return _wrappedprovider.Description

End Get

End Property

Public Overrides Property ApplicationName() As String

Get

Return _wrappedprovider.ApplicationName

End Get

Set(ByVal value As String)

_wrappedprovider.ApplicationName = value

End Set

End Property

#End Region

#Region "Methods"

Public Overloads Overrides Sub Initialize(ByVal name As String, ByVal config As System.Collections.Specialized.NameValueCollection)

If config Is Nothing Then

Throw New ArgumentNullException("config")

End If

Dim _functionProviderType As Type = Type.[GetType](config("rolefunctionalProvider"))

config.Remove("rolefunctionalProvider")

Dim instance As Object = Activator.CreateInstance(_functionProviderType)

_wrappedprovider = DirectCast(instance, RoleProvider)

_wrappedprovider.Initialize(name, config)

End Sub

End Class

 

The Keyboard Cowboy
Top 100 Contributor
Posts 78
Aaron replied on Thu, Jun 26 2008 11:16 AM
oh yea.. it errors out with a ArgumentNullException
The Keyboard Cowboy
Top 10 Contributor
Posts 629
skagen00 replied on Thu, Jun 26 2008 11:37 AM

So the line that it's bombing out on is this one, right?

            _wrappedprovider.Initialize(name, config);

If _wrappedprovider does get created as a SqlRoleProvider, the implementation for Initialize is below (via Reflector).

Is your ArgumentNullException's argument "config"? If so, you may be passing in an empty config array (look at the code below for SqlRoleProvider). Mine has 3 values for applicationName, connectionStringName, and description respectively.

 

Chris

 

public override void Initialize(string name, NameValueCollection config)
{
    HttpRuntime.CheckAspNetHostingPermission(AspNetHostingPermissionLevel.Low, "Feature_not_supported_at_this_level");
    if (config == null)
    {
        throw new ArgumentNullException("config");
    }
    if (string.IsNullOrEmpty(name))
    {
        name = "SqlRoleProvider";
    }
    if (string.IsNullOrEmpty(config["description"]))
    {
        config.Remove("description");
        config.Add("description", SR.GetString("RoleSqlProvider_description"));
    }
    base.Initialize(name, config);
    this._SchemaVersionCheck = 0;
    this._CommandTimeout = SecUtility.GetIntValue(config, "commandTimeout", 30, true, 0);
    string specifiedConnectionString = config["connectionStringName"];
    if ((specifiedConnectionString == null) || (specifiedConnectionString.Length < 1))
    {
        throw new ProviderException(SR.GetString("Connection_name_not_specified"));
    }
    this._sqlConnectionString = SqlConnectionHelper.GetConnectionString(specifiedConnectionString, true, true);
    if ((this._sqlConnectionString == null) || (this._sqlConnectionString.Length < 1))
    {
        throw new ProviderException(SR.GetString("Connection_string_not_found", new object[] { specifiedConnectionString }));
    }
    this._AppName = config["applicationName"];
    if (string.IsNullOrEmpty(this._AppName))
    {
        this._AppName = SecUtility.GetDefaultAppName();
    }
    if (this._AppName.Length > 0x100)
    {
        throw new ProviderException(SR.GetString("Provider_application_name_too_long"));
    }
    config.Remove("connectionStringName");
    config.Remove("applicationName");
    config.Remove("commandTimeout");
    if (config.Count > 0)
    {
        string key = config.GetKey(0);
        if (!string.IsNullOrEmpty(key))
        {
            throw new ProviderException(SR.GetString("Provider_unrecognized_attribute", new object[] { key }));
        }
    }
}
Top 100 Contributor
Posts 78
Aaron replied on Thu, Jun 26 2008 12:06 PM

Are you using the Microsoft.Samples. Because I am not.  Is it better to be using them?

The Keyboard Cowboy
Top 10 Contributor
Posts 629
skagen00 replied on Thu, Jun 26 2008 12:14 PM

Not using Microsoft.Samples code, nope. I just grabbed the code for System.Web.Security.SqlRoleProvider using reflector.

 

 

Top 100 Contributor
Posts 78
Aaron replied on Thu, Jun 26 2008 12:35 PM

Alright well anyways... The check for the config being nothing is passed and that thrown exception for the ArgumentNullException isn't being thrown at that point. It's being thrown after the Initialization Method when the Property for the Name is completed.

The Keyboard Cowboy
Page 2 of 3 (31 items) < Previous 1 2 3 Next > | RSS

Copyright (c) 2006-2010 Marimer LLC. All rights reserved.
Email admin@lhotka.net for support.
Powered by Community Server (Non-Commercial Edition), by Telligent Systems