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: forums.lhotka.net/forums/t/11200.aspx


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,

ChinaRay

 

                            Test code:

                               

                                KsPrincipal.Logout();

 

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

 

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

 

                                if (user.Identity.IsAuthenticated)

                                {

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

                                }

                                else

                                {

                                        MemberUtility.Log("Not logged in");

                                }

 

 

  [Serializable()]

  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()

          {  }

 

          #endregion

 

          #region  Data Access

 

          [Serializable()]

          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)

                          return;

 

                  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)

                                  roleList.Add(role);

                          Roles = roleList;

                  }

          }

          #endregion

  }

 

 

         [Serializable]

        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)

                {

                        SetPrincipal(KsIdentity.GetIdentity(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();

                }

        }

       

<configuration>

        <appSettings>

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

        </appSettings>

       

        <connectionStrings>

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

        </connectionStrings>

 

        <system.web>

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

                <roleManager enabled="true">

                        <providers>

                                <clear/>

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

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

                        </providers>

                </roleManager>

 

                <!--

                <authentication mode="Forms"/>

                -->

 

                <membership>

                        <providers>

                                <remove name="AspNetSqlMembershipProvider"/>

                                <add name="AspNetSqlMembershipProvider"

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

                                                connectionStringName="AspNetDB"

                                                enablePasswordRetrieval="false"

                                                enablePasswordReset="true"

                                                requiresQuestionAndAnswer="false"

                                                applicationName="/"

                                                requiresUniqueEmail="false"

                                                passwordFormat="Hashed"

                                                maxInvalidPasswordAttempts="5"

                                                minRequiredPasswordLength="1"

                                                minRequiredNonalphanumericCharacters="0"

                                                passwordAttemptWindow="10"

                                                passwordStrengthRegularExpression="" />

                        </providers>

                </membership>

        </system.web>

</configuration>

       

 

 

 

 

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