CSLA .NET

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

Forum has moved

New location: CSLA .NET forum


CSLA .NET Resources:
  • CSLA .NET forum
  • CSLA .NET home page
  • How can I write a custom rule like IsInRole and IsNotInRole

    Answered (Verified) This post has 1 verified answer | 10 Replies | 2 Followers

    Top 200 Contributor
    45 Posts
    SreeRamaSaran Mullapudi posted on Thu, Dec 22 2011 4:14 AM

    Hi,

    I've to check a value that exists in a blocked list or not for the current signed in user. For this I want to write a custom rule just like IsInRole and IsNotInRole. Could any one provide me an example?

     

    Thanks & Regards,

    SreeRam. 

    Answered (Verified) Verified Answer

    Top 10 Contributor
    2,279 Posts

    Csla.ApplicationContext.User is defined as IPrincipal and as such you will only see the properties defined in that Interface.

    You must cast that object into TruIdentity in order to see the properties implemented in that class.

    var identity = (TruIdentity) Csla.ApplicationContext.User;

    // can accces properties on identity object.

     

     

    Jonny Bekkum, Norway CslaContrib Coordinator

    All Replies

    Top 10 Contributor
    2,279 Posts
    Suggested by JonnyBee

    Look at Samples\net\cs\businessruledemo Project and the OnlyForUS rule

    or Samples\net\cs\ruletutorial sample (has many custom business rules and authorization rules).

    Jonny Bekkum, Norway CslaContrib Coordinator

    Top 200 Contributor
    45 Posts

    I've gone through the examples. But I didn't understand to attach the rule to CustomIdentity class. I'm using the CustomMembershipProvider. And I'm unable to access the properties of my Identity class using Csla.ApplicationContext.User.Identity.

    My Identity class code

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using Csla;

    using Csla.Core;

    using Csla.Security;

    using Csla.Serialization;

    using LX.Dal;

     

    namespace LX.Library.Security

    {

        [Serializable]

        public class TruIdentity : CslaIdentityBase<TruIdentity>

        {

            public static readonly PropertyInfo<int> UserIdProperty = RegisterProperty<int>(c => c.UserId);

            public int UserId

            {

                get { return GetProperty(UserIdProperty); }

                private set { LoadProperty(UserIdProperty, value); }

            }

     

            public static readonly PropertyInfo<int> CompanyIdProperty = RegisterProperty<int>(c => c.CompanyId);

            public int CompanyId

            {

                get { return GetProperty(CompanyIdProperty); }

                private set { LoadProperty(CompanyIdProperty, value); }

            }

     

            public static readonly PropertyInfo<byte> CompanyTypeIdProperty = RegisterProperty<byte>(c => c.CompanyTypeId);

            public byte CompanyTypeId

            {

                get { return GetProperty(CompanyTypeIdProperty); }

                private set { LoadProperty(CompanyTypeIdProperty, value); }

            }

     

            public static readonly PropertyInfo<byte> AccountTypeIdProperty = RegisterProperty<byte>(c => c.AccountTypeId);

            public byte AccountTypeId

            {

                get { return GetProperty(AccountTypeIdProperty); }

                private set { LoadProperty(AccountTypeIdProperty, value); }

            }

     

            public static readonly PropertyInfo<string> FullNameProperty = RegisterProperty<string>(c => c.FullName);

            public string FullName

            {

                get { return GetProperty(FullNameProperty); }

                private set { LoadProperty(FullNameProperty, value); }

            }

     

            public static readonly PropertyInfo<string> EmailIdProperty = RegisterProperty<string>(c => c.EmailId);

            public string EmailId

            {

                get { return GetProperty(EmailIdProperty); }

                private set { LoadProperty(EmailIdProperty, value); }

            }

     

            public static readonly PropertyInfo<string> MobileProperty = RegisterProperty<string>(c => c.Mobile);

            public string Mobile

            {

                get { return GetProperty(MobileProperty); }

                private set { LoadProperty(MobileProperty, value); }

            }

     

            public static readonly PropertyInfo<List<int>> BlockedListProperty = RegisterProperty<List<int>>(c => c.BlockedList);

            public List<int> BlockedList

            {

                get { return GetProperty(BlockedListProperty); }

                private set { LoadProperty(BlockedListProperty, value); }

            }

     

            public static void GetTruIdentity(

                string username, string password, EventHandler<DataPortalResult<TruIdentity>> callback)

            {

                DataPortal.BeginFetch<TruIdentity>(new UsernameCriteria(username, password), callback);

            }

    #if !SILVERLIGHT

     

            public static TruIdentity GetTruIdentity(string username, string password)

            {

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

            }

     

            internal static TruIdentity GetTruIdentity(string username)

            {

                return DataPortal.Fetch<TruIdentity>(username);

            }

     

            #region Data Access Methods

     

            private void DataPortal_Fetch(UsernameCriteria criteria)

            {

                AuthenticationType = "Custom";

                using (var ctx = DalFactory.GetManager())

                {

                    var dal = ctx.GetProvider<IIdentityDal>();

                    var companyDal = ctx.GetProvider<ICompanyDal>();

                    if (dal.VerifyUser(criteria.Username, criteria.Password))

                        LoadUserData(criteria.Username, dal, companyDal);

                }

            }

     

            private void DataPortal_Fetch(string username)

            {

                AuthenticationType = "Custom";

                using (var ctx = DalFactory.GetManager())

                {

                    var dal = ctx.GetProvider<IIdentityDal>();

                    var companyDal = ctx.GetProvider<ICompanyDal>();

     

                    LoadUserData(username, dal, companyDal);

                    LoadCompanyInfo(ctx);

                }

            }

     

            private void LoadCompanyInfo(IDalManager ctx)

            {

                if (IsAuthenticated)

                {

                    var dal = ctx.GetProvider<ICompanyUserDal>();

                    var companyDal = ctx.GetProvider<ICompanyDal>();

                    var companyData = companyDal.Fetch(dal.GetCompanyId(UserId));

                    CompanyId = companyData.CompanyId;

                    CompanyTypeId = companyData.CompanyTypeId;

                    AccountTypeId = companyData.AccountTypeId;

                }

            }

     

            private void LoadUserData(string username, IIdentityDal dal, ICompanyDal companyDal)

            {

                var userData = dal.GetUser(username);

                IsAuthenticated = (userData != null);

                //var companyData = companyDal.Fetch()

                if (IsAuthenticated)

                {

                    UserId = userData.UserId;

                    Name = userData.Username;

                    FullName = string.Concat(userData.FirstName, " ", userData.LastName);

                    Roles = new MobileList<string>(dal.GetRoles(userData));

                    EmailId = userData.EmailId;

                }

            }

            #endregion

     

    #endif

        }

    }

     

    I'm unable to access any of these properties other than the properties available in CslaIdentityBase

     

    Thanks,

    SreeRam

    Top 10 Contributor
    2,279 Posts

    Csla.ApplicationContext.User is defined as IPrincipal and as such you will only see the properties defined in that Interface.

    You must cast that object into TruIdentity in order to see the properties implemented in that class.

    var identity = (TruIdentity) Csla.ApplicationContext.User;

    // can accces properties on identity object.

     

     

    Jonny Bekkum, Norway CslaContrib Coordinator

    Not Ranked
    3 Posts
    GaryB replied on Sat, Dec 31 2011 12:20 AM

    I have a similar problem where I am trying to override the IsInRole function in my custom principal object as I have read in one of Rocky's books but when I try to cast Csla.ApplicationContext.User to my custom identity object I get a run time error.  Any suggestions where I should look?

    Top 10 Contributor
    2,279 Posts

    Hi,

    1. Make sure to set CslaAuthentication in app.config/web.config to anything but Windows. (You are not using Windows authentication)
    2. Create your own boot strap that assigns your custom serializable principal to Csla.ApllicationContext.User.  (use CslaIdentity and CslaPrincipal as base classes)
    3. If using ASP.NET make sure that Csla.Web.dll is present in bin folder
    4. If using WPF make sure Csla.Xaml.dll is present in bin folder

    The principlal object is stored differently for ASP.NET and WPF, depending on the presence of these dll's.

    The IsInRole / IsNotInRole rules should the call overridden IsInRole method on your custom principal object straight away.

    Jonny Bekkum, Norway CslaContrib Coordinator

    Not Ranked
    3 Posts
    GaryB replied on Sun, Jan 1 2012 5:12 PM

    Hi JonnyBee,

    Thanks for your quick response.  I am a Csla newbie so I have lots of questions and am experimenting with a number of techniques.  My app.config file indeed did not contain the CslaAuthentication key.  I added it but still get the same error.  I have created my own bootstrap that assigns my custom principal to Csla,ApplicationContext.User.  I will include code for both my pricipal and identity below.  I am currently testing this using windows forms because that is the environment I am most comfortable with the intent of moving to Silverlight once I learn the ins and out of Csla so I assume I am ok with only the Csla.dll in my Bin folder.  As mentioned here is the code which may give you a hint of what is happening.  Thanks alot and Happy New Year to you!

    using System;
    using System.Security.Principal;
    using Csla.Security;
    using Csla.Serialization;

    namespace AgroManager.Library.Security
    {
        [Serializable]
        public class amPrincipal : CslaPrincipal
        {
            public amPrincipal()
            { }

            protected amPrincipal(IIdentity identity)
                : base(identity)
            { }

            public static void BeginLogin(string username, string password)
            {
                amIdentity.GetAMIdentity(username, password, (o, e) =>
                {
                    if (e.Error == null && e.Object != null)
                        SetPrincipal(e.Object);
                    else
                        Logout();
                });
            }

    #if !SILVERLIGHT
            public static bool Login(string username, string password)
            {
                var identity = amIdentity.GetAMIdentity(username, password);
                return SetPrincipal(identity);
            }

            public static bool Load(string username)
            {
                var identity = amIdentity.GetAMIdentity(username);
                return SetPrincipal(identity);
            }
    #endif

            private static bool SetPrincipal(IIdentity identity)
            {
                if (identity.IsAuthenticated)
                {
                    amPrincipal principal = new amPrincipal(identity);
                    Csla.ApplicationContext.User = principal;
                }
                OnNewUser();
                return identity.IsAuthenticated;
            }

            public static void Logout()
            {
                Csla.ApplicationContext.User = new UnauthenticatedPrincipal();
                OnNewUser();
            }

            public static event Action NewUser;
            private static void OnNewUser()
            {
                if (NewUser != null)
                    NewUser();
            }

            public override bool IsInRole(string role)
            {
                var identity = (amIdentity) Csla.ApplicationContext.User;
                return identity.Roles.Contains(role);
                // can accces properties on identity object.


                //return base.IsInRole(role);
              
            }
        }
    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Csla;
    using Csla.Serialization;
    using Csla.Security;

    namespace AgroManager.Library
    {
        [Serializable]
        public class amIdentity : CslaIdentityBase<amIdentity>
        {
            public static readonly PropertyInfo<int> MasterAccountIdProperty = RegisterProperty<int>(c => c.MasterAccountId);
            public int MasterAccountId
            {
                get { return GetProperty(MasterAccountIdProperty); }
                private set { LoadProperty(MasterAccountIdProperty, value); }
            }

            public static readonly PropertyInfo<int> MasterUserIdProperty = RegisterProperty<int>(c => c.MasterUserId);
            public int MasterUserId
            {
                get { return GetProperty(MasterUserIdProperty); }
                private set { LoadProperty(MasterUserIdProperty, value); }
            }

            public static readonly PropertyInfo<int> SessionIdProperty = RegisterProperty<int>(c => c.SessionId);
            public int SessionId
            {
                get { return GetProperty(SessionIdProperty); }
                private set { LoadProperty(SessionIdProperty, value); }
            }

            public static readonly PropertyInfo<string > FirstNameProperty = RegisterProperty<string >(c => c.FirstName);
            public string FirstName
            {
                get { return GetProperty(FirstNameProperty); }
                private set { LoadProperty(FirstNameProperty, value); }
            }

            public static readonly PropertyInfo<string> LastNameProperty = RegisterProperty<string>(c => c.LastName);
            public string LastName
            {
                get { return GetProperty(LastNameProperty); }
                private set { LoadProperty(LastNameProperty, value); }
            }

            public static readonly PropertyInfo<string> EmailProperty = RegisterProperty<string>(c => c.Email);
            public string Email
            {
                get { return GetProperty(EmailProperty); }
                private set { LoadProperty(EmailProperty, value); }
            }

            public static readonly PropertyInfo<string> PhoneNumberProperty = RegisterProperty<string>(c => c.PhoneNumber);
            public string PhoneNumber
            {
                get { return GetProperty(PhoneNumberProperty); }
                private set { LoadProperty(PhoneNumberProperty, value); }
            }      

            public static void GetAMIdentity(string username, string password, EventHandler<DataPortalResult<amIdentity>> callback)
            {
                DataPortal.BeginFetch<amIdentity>(new UsernameCriteria(username, password), callback);
            }

    #if !SILVERLIGHT
            public static amIdentity GetAMIdentity(string username, string password)
            {
                return DataPortal.Fetch<amIdentity>(new UsernameCriteria(username, password));
            }

            internal static amIdentity GetAMIdentity(string username)
            {
                return DataPortal.Fetch<amIdentity>(username);
            }

            private void DataPortal_Fetch(string username)
            {
                AgroManager.Dal.UserDto data = null;
                using (var ctx = AgroManager.Dal.DalFactory.GetManager())
                {
                    var dal = ctx.GetProvider <AgroManager.Dal.IUserDal>();
                    try
                    {
                        data = dal.Fetch(username);
                    }
                    catch (AgroManager.Dal.DataNotFoundException)
                    {
                        data = null;
                    }
                    LoadUser(data);
                }
            }

            private void DataPortal_Fetch(UsernameCriteria criteria)
            {
                AgroManager.Dal.UserDto data = null;
                using (var ctx = AgroManager.Dal.DalFactory.GetManager())
                {
                    var dal = ctx.GetProvider<AgroManager.Dal.IUserDal>();
                    try
                    {
                        data = dal.Fetch(criteria.Username, criteria.Password);
                    }
                    catch (AgroManager.Dal.DataNotFoundException)
                    {
                        data = null;
                    }
                    LoadUser(data);
                }
            }

            private void LoadUser(AgroManager.Dal.UserDto data)
            {
                if (data != null)
                {
                    if (data.ErrorResourceId == string.Empty  )
                    {
                        base.Name = data.Username;
                        base.IsAuthenticated = true;
                        base.AuthenticationType = "Membership";
                        base.Roles = new Csla.Core.MobileList<string>(data.Roles);
                        MasterAccountId = data.MasterAccountId;
                        MasterUserId = data.MasterUserId ;
                        FirstName = data.Firstname;
                        LastName = data.Lastname;
                        SessionId = data.SessionId;
                        Email = data.EmailAddress;
                        PhoneNumber = data.PhoneNumber;
                    }
                    else
                    {
                        base.Name = string.Empty;
                        base.IsAuthenticated = false;
                        base.AuthenticationType = string.Empty;
                        base.Roles = new Csla.Core.MobileList<string>();
                        MasterAccountId = 0;
                        MasterUserId = 0;
                        FirstName = string.Empty;
                        LastName = string.Empty;
                        SessionId = 0;
                        Email = string.Empty;
                        PhoneNumber = string.Empty;
                    }

                }
                else
                {
                    base.Name = string.Empty;
                    base.IsAuthenticated = false;
                    base.AuthenticationType = string.Empty;
                    base.Roles = new Csla.Core.MobileList<string>();
                    MasterAccountId = 0;
                    MasterUserId = 0;
                    FirstName = string.Empty;
                    LastName = string.Empty;
                    SessionId = 0;
                    Email = string.Empty;
                    PhoneNumber = string.Empty;   
                }
            }
    #endif
        }
    }

    Top 10 Contributor
    2,279 Posts
    Suggested by GaryB

    Hi,

    This is your own coding error.

    Csla.ApplicationContext.User is of type IPrincipal and not an IIdentity.

            public override bool IsInRole(string role)
            {
                var principal = (amPrincipal) Csla.ApplicationContext.User;
                var identity = (amIdentity) Csla.ApplicationContext.User.Identity;

                // can accces properties on identity object.

                return base.IsInRole(role);
            }

    Although for using the standard IsInRole method implemented in CslaIdentity and CslaPrincipal you do NOT need to override the IsInRole method.

    Jonny Bekkum, Norway CslaContrib Coordinator

    Not Ranked
    3 Posts
    GaryB replied on Sun, Jan 1 2012 11:00 PM

    Thanks so much.  You are right on both counts.  I attempted to override the IsInRole method because it didn't appear that it was returning the correct answer.  In the end it was simply because of pad characters returned from the database that it didn't match the role.  The good part is that I learned more about the principal, identity, and Clsa.ApplicationContext.User object which I needed to know eventually anyhow.

    Thanks again,

    Gary

    Not Ranked
    3 Posts

    I realize this is an old post but I only came across two posts (both old) and I am a little confused now.

    I am using Csla 4.1.0 and seem to be having a cast issue.  Same scenario as the OP where I am trying to get to custom properties that have been implemented into my CustomIdentity class.

    When I try to cast Csla.ApplicationContext.User.Identity to CustomIdentity, I get "Unable to cast object of type 'System.Security.Principal.GenericPrincipal' to 'DMS.Library.Security.CustomIdentity.'

    Here's the code in question:

    public int UserOrgCount {

                get {

                    var identity = (CustomIdentity)Csla.ApplicationContext.User.Identity;

                    return identity.Orgs.Count();

                }

            }

    I thought I followed the ebook pretty well and in fact everything seems to be working.  The only thing I want to get access to now is the Identity properties so that I can replace individual database calls with data that's already been loaded into my custom properties.

    Let me know if there is any other information I can provide.

    For reference, here is my CustomPrincipal and CustomIdentity objects:

     

    using System;

    using System.Security.Principal;

    using Csla.Security;

    using Csla.Serialization;

     

    namespace DMS.Library.Security {

        [Serializable]

        public class CustomPrincipal : CslaPrincipal {

            private CustomPrincipal(IIdentity identity)

                : base(identity) { }

     

            /// <summary>

            /// 

            /// </summary>

            /// <param name="username">The user's username</param>

            /// <param name="password">The user's password</param>

            /// <param name="completed"></param>

            public static void BeginLogin(string username, string password, Action<Exception> completed) {

                DMS.Library.Security.CustomIdentity.GetCustomIdentity(username, password, (o, e) => {

                    if (e.Error != null)

                        Logout();

                    else

                        Csla.ApplicationContext.User = new CustomPrincipal(e.Object);

                    completed(e.Error);

                });

            }

     

    #if !SILVERLIGHT

     

            /// <summary>

            /// NOT IMPLEMENTED - Attempts to login the user with their username and password

            /// </summary>

            /// <param name="pUserName">The user's username</param>

            /// <param name="pPassword">The user's password</param>

            /// <remarks>

            /// As of 05/07/2015, this function is not used because it will create a circular execution of code.  

            /// Validation of credentials is handled within the ValidateUser function of each implemented Membership provider.

            /// Assuming the ValidateUser function returns true, the Principal's Load function is used instead

            /// </remarks>

            public static void Login(string username, string password) {

                throw new NotImplementedException("The CustomPrincipal Login method has not been implemented.  Refer to the remarks section of method's code for an explanation.");

                //var identity = DMS.Library.Security.CustomIdentity.GetCustomIdentity(username, password);

                //Csla.ApplicationContext.User = new CustomPrincipal(identity);

            }

     

            public static void Load(string username) {

                var identity = DMS.Library.Security.CustomIdentity.GetCustomIdentity(username);

                Csla.ApplicationContext.User = new CustomPrincipal(identity);

            }

     

    #endif

     

            /// <summary>

            /// Replaces the current CustomPrincipal object with a new one that has an UnauthenticatedIdentity loaded up.

            /// </summary>

            public static void Logout() {

                Csla.ApplicationContext.User = new UnauthenticatedPrincipal();

            }

        }

    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Csla;
    using Csla.Security;
    using Csla.Serialization;
    using DMS.Library.Admin;
    namespace DMS.Library.Security {
        [Serializable]
        public class CustomIdentity : CslaIdentityBase<CustomIdentity> {
            
            #region Properties
                public static readonly PropertyInfo<string> EmailProperty = RegisterProperty<string>(c => c.Email);
                /// <summary>
                /// The user's E-mail
                /// </summary>
                public string Email {
                    get { return GetProperty(EmailProperty); }
                    private set { LoadProperty(EmailProperty, value); }
                }
                public static readonly PropertyInfo<Guid> UserIdProperty = RegisterProperty<Guid>(p => p.UserId);
                /// <summary>
                /// The user's internal identifier
                /// </summary>
                public Guid UserId {
                    get { return GetProperty(UserIdProperty); }
                    private set { LoadProperty(UserIdProperty, value); }
                }
                public static readonly PropertyInfo<string> FirstNameProperty = RegisterProperty<string>(c => c.FirstName);
                /// <summary>
                /// The user's first name
                /// </summary>
                public string FirstName {
                    get { return GetProperty(FirstNameProperty); }
                    private set { LoadProperty(FirstNameProperty, value); }
                }
                public static readonly PropertyInfo<string> LastNameProperty = RegisterProperty<string>(c => c.LastName);
                /// <summary>
                /// The user's last name
                /// </summary>
                public string LastName {
                    get { return GetProperty(LastNameProperty); }
                    private set { LoadProperty(LastNameProperty, value); }
                }
                public static readonly PropertyInfo<string> FullNameProperty = RegisterProperty<string>(c => c.FullName);
                /// <summary>
                /// The user's full name
                /// </summary>
                public string FullName {
                    get { return GetProperty(FullNameProperty); }
                    private set { LoadProperty(FullNameProperty, value); }
                }
                public static readonly PropertyInfo<string> HomeOrgStringProperty = RegisterProperty<string>(p => p.HomeOrgString);
                /// <summary>
                /// The user's Home OrgString
                /// </summary>
                public string HomeOrgString {
                    get { return GetProperty(HomeOrgStringProperty); }
                    private set { LoadProperty(HomeOrgStringProperty, value); }
                }
                public static readonly PropertyInfo<bool> IsApprovedProperty = RegisterProperty<bool>(p => p.IsApproved);
                /// <summary>
                /// The original membership IsApproved column
                /// </summary>
                /// <remarks>
                /// Value comes from the aspnet_Membership tables
                /// </remarks>
                public bool IsApproved {
                    get { return GetProperty(IsApprovedProperty); }
                    private set { LoadProperty(IsApprovedProperty, value); }
                }
                public static readonly PropertyInfo<List<string>> OrgsProperty = RegisterProperty<List<string>>(p => p.Orgs);
                /// <summary>
                /// Returns the list of Departments that the user belongs to
                /// </summary>
                public List<string> Orgs {
                    get { return GetProperty(OrgsProperty); }
                    private set { LoadProperty(OrgsProperty, value); }
                }
                public static readonly PropertyInfo<string> AppNameProperty = RegisterProperty<string>(p => p.AppName);
                public string AppName {
                    get { return "AppName"; }
                }
        #endregion
            public static void GetCustomIdentity(string username, string password, EventHandler<DataPortalResult<CustomIdentity>> callback) {
                DataPortal.BeginFetch<CustomIdentity>(new UsernameCriteria(username, password), callback);
            }
    #if !SILVERLIGHT
            /// <summary>
            /// Gets the user after authenticating them with their username and password
            /// </summary>
            /// <param name="pUserName">The user's employee number</param>
            /// <param name="pPassword">The user's password</param>
            /// <returns></returns>
            public static CustomIdentity GetCustomIdentity(string username, string password) {
                return DataPortal.Fetch<CustomIdentity>(new UsernameCriteria(username, password));
            }
            
            private void DataPortal_Fetch(UsernameCriteria criteria) {
                AuthenticationType = "Custom";
                using (var mgr = DataAccess.DalFactory.GetManager()) {
                    var dal = mgr.GetProvider<DataAccess.IIdentityDal>();
                    if (dal.VerifyUser(criteria.Username, criteria.Password))
                        LoadUserData(criteria.Username, dal);
                }
            }
            /// <summary>
            /// Gets the user based on their username
            /// </summary>
            /// <param name="pUserName">The user's employee number</param>
            /// <returns></returns>
            internal static CustomIdentity GetCustomIdentity(string username) {
                return DataPortal.Fetch<CustomIdentity>(username);
            }
            private void DataPortal_Fetch(string username) {
                AuthenticationType = "Custom";
                using (var mgr = DataAccess.DalFactory.GetManager()) {
                    var dal = mgr.GetProvider<DataAccess.IIdentityDal>();
                    LoadUserData(username, dal);
                }
            }
            /// <summary>
            /// Uses the DataAccess DalFactory to dynamically load the required membership provider
            /// </summary>
            /// <param name="pUserName">The user's employee number</param>
            /// <param name="dal">The Identity's data access object</param>
            private void LoadUserData(string pUserName, DataAccess.IIdentityDal dal) {
                var userData = dal.GetUser(pUserName);
                //Step 1 authentication loading - If the user exists in the membership tables, the user is temporarily authenticated
                this.IsAuthenticated = (userData != null);
                
                if (this.IsAuthenticated) {
                    this.Name = userData.UserName;
                    this.FirstName = userData.FirstName;
                    this.LastName = userData.LastName;
                    this.FullName = string.Format("{0} {1}", userData.FirstName, userData.LastName);
                    this.Email = userData.Email;
                    this.UserId = userData.UserId;
                    this.HomeOrgString = userData.HomeOrgString;
                    this.IsApproved = userData.IsApproved;
                    //Step 2 authentication loading - Update the Identity's IsAuthenticated property based on the membership table
                    this.IsAuthenticated = this.IsApproved;
                    //Initialize the MobileList property and then load up the roles once we've gotten them
                    this.Roles = new Csla.Core.MobileList<string>();
                    string[] _roles = UserRole.GetRolesForUser(pUserName, string.Empty, true);
                    foreach (var role in _roles) {
                        this.Roles.Add(role);
                    }
                    //Load up the Orgs the user belongs to
                    this.Orgs = UserOrg.GetOrgsForUser(pUserName, true, false).ToList();
                    //Step 3 authentication loading - Check to make sure that even if the user is authenticated with AD and exists in the membership table, the user has at least 1 Org and Role
                    if (!(this.Orgs.Count() > 0 && this.Roles.Count() > 0)) {
                        this.IsAuthenticated = false;
                        throw new InvalidOperationException(string.Format("User {0} does not have access to any departments and/or roles.\n    Org Count: {1};\n    Role Count: {2}", this.Name, this.Orgs.Count(), this.Roles.Count()));
                    }
                }
            }
    #endif
        }
    }
    Top 10 Contributor
    2,279 Posts

    Hi,

    Please start a new thread as this thread is already marked as answered and you are not adding anything to the answer. 

    Jonny Bekkum, Norway CslaContrib Coordinator

    Page 1 of 1 (11 items) | RSS

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