Question: using M$ MemberShip API in a CSLA.NET for WinForm App

Question: using M$ MemberShip API in a CSLA.NET for WinForm App

Old forum URL:

chinaray posted on Monday, February 27, 2012

Dear Sir, 

I am trying to use ASP.NET 2.0 in a winform application.

Because I don’t need create a membership db and can use existing membership API.

Below is my custom code for Principle and Identity, it looks works.

if you are free, could you please give a look,

I wonder to know whether I am in the right way, any better way?

By the way, I cannot use your MembershipTest code in a winform, fail, don’t know why.


Best wishes,



                            Test code:




                                KsPrincipal.Login("admin", "123456=");


                                System.Security.Principal.IPrincipal user = Csla.ApplicationContext.User;


                                if (user.Identity.IsAuthenticated)


                                        MemberUtility.Log("Logged in as " + user.Identity.Name);                                       




                                        MemberUtility.Log("Not logged in");





  public class KsIdentity : CslaIdentity


          #region  Factory Methods


          internal static KsIdentity GetIdentity(string username, string password)


                  return DataPortal.Fetch<KsIdentity>(new UsernameCriteria(username, password));



          internal static KsIdentity GetIdentity(string username)


                  MessageBox.Show("GetIdentity UnImplemented");

                  return null;

                  //return DataPortal.Fetch<PTIdentity>(new LoadOnlyCriteria(username));



          private KsIdentity()

          {  }




          #region  Data Access



          private class LoadOnlyCriteria


                  private string mUsername;

                  public string Username {  get  { return mUsername; }  }


                  public LoadOnlyCriteria(string username)

                  {  mUsername = username; }



          private void DataPortal_Fetch(UsernameCriteria criteria)


                  Name = "";

                  IsAuthenticated = false;

                  Roles = new Csla.Core.MobileList<string>();


                  System.Web.Security.Membership.ApplicationName = "kuaishou"; //important

                  System.Web.Security.MembershipUser m_defaultUser = null;

                  m_defaultUser = System.Web.Security.Membership.GetUser(criteria.Username);

                  if (m_defaultUser == null)



                  bool bValid = System.Web.Security.Membership.ValidateUser(criteria.Username, criteria.Password);


                  if (bValid == true)


                          Name = criteria.Username;

                          IsAuthenticated = true;


                          var roleList = new Csla.Core.MobileList<string>();


                          System.Web.Security.MembershipUser membershipUser = System.Web.Security.Membership.GetUser();

                          string[] roles = System.Web.Security.Roles.GetRolesForUser(criteria.Username);

                          foreach (var role in roles)


                          Roles = roleList;








        public class KsPrincipal : BusinessPrincipalBase


                protected KsPrincipal(IIdentity identity) : base(identity)




                //public static KsPrincipal Login(string username, string password)


                //    var identity = MembershipIdentity.GetMembershipIdentity<MembershipIdentity>(username, password, true);

                //    return new KsPrincipal(identity);



                public static bool Login(string username, string password)


                        return SetPrincipal(KsIdentity.GetIdentity(username, password));



                public static void LoadPrincipal(string username)





                private static bool SetPrincipal(KsIdentity identity)


                        if (identity.IsAuthenticated)


                                KsPrincipal principal = new KsPrincipal(identity);

                                Csla.ApplicationContext.User = principal;


                        return identity.IsAuthenticated;



                public static void Logout()


                        Csla.ApplicationContext.User = new UnauthenticatedPrincipal();






                <add key="CslaAuthentication" value="Csla" />




                <add name="AspNetDB" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=aspnetdb;Integrated Security=SSPI" />




                <!-- <roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider"/> -->

                <roleManager enabled="true">



                                <add connectionStringName="AspNetDB" applicationName="kuaishou" name="AspNetSqlRoleProvider"

                                                type="System.Web.Security.SqlRoleProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>





                <authentication mode="Forms"/>





                                <remove name="AspNetSqlMembershipProvider"/>

                                <add name="AspNetSqlMembershipProvider"

                                                type="System.Web.Security.SqlMembershipProvider, System.Web, Version=, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"












                                                passwordStrengthRegularExpression="" />










RockfordLhotka replied on Wednesday, February 29, 2012

Have you read through the section on this topic in the Using CSLA 4: Data Portal Configuration book? The specific base classes have changed over the past few versions of CSLA, but the basic code and concepts are the same.

Copyright (c) Marimer LLC