Access Denied When trying to view conversations

Access Denied When trying to view conversations

This question is answered

Hi All,

We are creating a number of users programatically in our TC5 community. When a users starts a conversation with another user, then the other user recieves an access denied error when trying to view the conversation. I believe there is a permission or property that is not being set on the users when we are creating them programatically, as this issue does not occur with users created through the normal registration process. Any assistance with this would be greatly appreciated.

Thanks

Adam

Verified Answer
  • Can you provide the code you're using to create a user so I can try and reproduce locally.

  •  Hi Alex,

    Please find below the code we are using to create our users. The NewUserRegistration() method is passed a custom cpuser object; but you can just ignore this and set the username, displayname and email to anything you like.

    Our code is based on the OOTB code from TC5, however I notice the OOTB code is calling: SaveAdditionalProfileData(user); and also setting a lot more values such as:

    user.AllowSiteToContact, user.Profile.DateFormat, user.EnableThreadTracking, user.EnableDisplayInMemberList, user.EnableHtmlEmail, user.EditorType, user.EditorType, user.PostViewType, user.PostSortOrder, user.EnableEmail, user.EnableUserSignatures, user.SetExtendedAttribute("CPShowHelpIcons", EnableHelpCheckBox.Checked.ToString());user.EnableFavoriteSharing, user.EnableUserAvatars; user.AddRssFeedUrl(url.Trim()); user.EnableStartConversations

    Do these values need setting or do they default? - If you can provide the bear minimum code to correctly create a user in TC5 and set the permissions/roles required to emulate and OOTB user creation then that would be great.

    ===start our code====

          csUser = NewUserRegistration(cpUser);

            /// <summary>
            /// Create a Telligent Community account for authenticated users
            /// </summary>
            /// <param name="cpUser">User</param>
            /// <returns>User</returns>
            public static User NewUserRegistration(CPUser cpUser)
            {
                // Switch context to admin user
                CSContext.Current.User = Users.GetUser(2100);

                try
                {
                    User csUser = new User();
                    csUser.Username = cpUser.CPUserName;
                    csUser.EnableDisplayName = true;
                    csUser.Profile.CommonName = cpUser.Username;
                    csUser.Password = "pa$$word";
                    csUser.Email = cpUser.CPEmail;

                    // Set TC specific properties required to create user
                    csUser.AccountStatus = UserAccountStatus.Approved;
                    csUser.IsAnonymous = false;

                    CreateUserStatus status = CreateNewUser(csUser);

                    if (status == CreateUserStatus.Created)
                    {

                       IRoleService roleService = Services.Get<IRoleService>();      

                       // Add standard OOTB TC roles 'Everyone' and 'Registered User'
                       Roles.AddUserToRole(cpUser.Username, "Everyone");
                       Roles.AddUserToRole(cpUser.Username, "Registered Users");


                        // Switch context back to current user
                        CSContext.Current.User = cpUser;

                        return csUser;
                    }

                    // Couldn't create user
                }
                catch
                {
                    // User not created
                   
                    // Switch context back to current user
                    CSContext.Current.User = cpUser;
                   
                    return null;
                }

                // Switch context back to current user
                CSContext.Current.User = cpUser;

                return null;
            }

            /// <summary>
            /// Creates the new user, returning the user creation status.
            /// </summary>
            /// <param name="user">User</param>
            /// <returns>CreateUserStatus</returns>
            public static CreateUserStatus CreateNewUser(User user)
            {
                // Attempt to create the user
                if (user == null || user.Username == "Anonymous")
                {
                    return CreateUserStatus.DuplicateUsername;
                }

                try
                {
                    return Users.Create(user, true);
                }
                catch (CSException exception)
                {
                    return exception.CreateUserStatus;
                }
                catch (Exception ex)
                {
                    return MemberRoleProfileProvider.Instance().Members.GetCreateUserStatus(ex);
                }
            }


    Thanks for your help.


    Adam

  • Hi Alex, I don't know if you have had chance to test my code, but If not would you be able to provide the most basic code required to create a user in tc5 and set all the required permissions to emulate registering a user using the standard createuser form?

    Thanks

    Adam

  • I haven't yet had a chance to test your code.

    The recommended approach is to create users via the REST API - telligent.com/.../users.aspx .  There is a sample Create User application in the Platform SDK which should help you here.

  • Hi Alex, I have spent all day looking at this and still can't seem to find a resolution.

     

    have tried the following:

    1) created 2 users using the REST API V2 - get the same access denied error when trying to access the conversations page

    2) created 2 users using the internal ip (using the OOTB code from the dev SDK for the control panel & create user forms) - get the same access denied error when trying to access the conversations page

    3) created two users in the control panel - get the same access denied error when trying to access the conversations page

    4) ensured:  Administration » Site Administration » Setup > enable conversations = true

    5) ensured user > site settings > enable conversations with = all users

    I can not find any permissions, roles or any other settings in the control panel which control conversations.

    What else can I do to debug this Alex?

    Is there a way to track the CSException / Security Service / CSModule that handles this and see what is throwing the 31 error code?

    Thanks

    Adam

     

     

  • Can you remove the CSException CSModule from your communityserver.config file.  You should then get an ASP.Net stack trace instead of a friendly error.  Can you post that stack trace.

    (N.B. make sure to restore the CSException module before going into production).

  • Hi Alex,

    I have done this, please see below. I have Googled to see if I could see what the error ment but don't seem to find anything useful. Any ideas?

    Its very strange as the conversation starter never has this issue and can see the conversations page fine, only the user who recieved the conversation gets the error:

    Server Error in '/' Application.

    --------------------------------------------------------------------------------

    Unhandled Execution Error

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: CommunityServer.Components.CSException:

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace:

    [CSException]

      CommunityServer.Messages.Messaging.GetConversation(Guid conversationId) +255

      CommunityServer.Messages.Controls.ConversationMessageList.get_DataSource() +466

      System.Web.UI.WebControls.Repeater.ConnectToDataSourceView() +183

      System.Web.UI.WebControls.Repeater.GetData() +13

      System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +220

      CommunityServer.Controls.WrappedRepeater.CreateControlHierarchy(Boolean useDataSource) +145

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.CreateControlHierarchy(Boolean useDataSource) +372

      System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51

      CommunityServer.Controls.WrappedRepeater.OnDataBinding(EventArgs e) +14

      System.Web.UI.WebControls.Repeater.DataBind() +75

      CommunityServer.Controls.WrappedRepeater.DataBind() +143

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.DataBind() +28

      System.Web.UI.Control.DataBindChildren() +211

      System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102

      System.Web.UI.Control.DataBind() +15

      System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +124

      System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443

      CommunityServer.Controls.WrappedRepeater.CreateControlHierarchy(Boolean useDataSource) +145

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.CreateControlHierarchy(Boolean useDataSource) +372

      System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51

      CommunityServer.Controls.WrappedRepeater.OnDataBinding(EventArgs e) +14

      System.Web.UI.WebControls.Repeater.DataBind() +75

      CommunityServer.Controls.WrappedRepeater.DataBind() +143

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.DataBind() +28

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.OnLoad(EventArgs e) +32

      System.Web.UI.Control.LoadRecursive() +50

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

    --------------------------------------------------------------------------------

    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

    Thanks

    Adam

  • This error is occuring because the current user is not a participant in the current conversation.

    Conversation conversation = Provider.GetConversation(conversationId);

    bool b = conversation.Participants.IsParticipant(CSContext.Current.User)

    The exception is occurring because that last line is returning false.  This means either CSContext.Current.User is null, or the conversation.Participants collection does not contain CSContext.Current.User.

    I know you have some code which temporarily changes CSContext.Current.User so make sure that isn't interfering with the method.  Otherwise who is listed on the conversation Participant collection?

  • Hi Alex,

    in the end it was indeed the context being changed that caused this issue. the context switching code which was used with the role service caused the conversations page to get confused and throw this error. It was a very difficult bug to track down but your response really helped point me in the right direction so thanks for that.

    Adam

All Replies
  • Can you provide the code you're using to create a user so I can try and reproduce locally.

  •  Hi Alex,

    Please find below the code we are using to create our users. The NewUserRegistration() method is passed a custom cpuser object; but you can just ignore this and set the username, displayname and email to anything you like.

    Our code is based on the OOTB code from TC5, however I notice the OOTB code is calling: SaveAdditionalProfileData(user); and also setting a lot more values such as:

    user.AllowSiteToContact, user.Profile.DateFormat, user.EnableThreadTracking, user.EnableDisplayInMemberList, user.EnableHtmlEmail, user.EditorType, user.EditorType, user.PostViewType, user.PostSortOrder, user.EnableEmail, user.EnableUserSignatures, user.SetExtendedAttribute("CPShowHelpIcons", EnableHelpCheckBox.Checked.ToString());user.EnableFavoriteSharing, user.EnableUserAvatars; user.AddRssFeedUrl(url.Trim()); user.EnableStartConversations

    Do these values need setting or do they default? - If you can provide the bear minimum code to correctly create a user in TC5 and set the permissions/roles required to emulate and OOTB user creation then that would be great.

    ===start our code====

          csUser = NewUserRegistration(cpUser);

            /// <summary>
            /// Create a Telligent Community account for authenticated users
            /// </summary>
            /// <param name="cpUser">User</param>
            /// <returns>User</returns>
            public static User NewUserRegistration(CPUser cpUser)
            {
                // Switch context to admin user
                CSContext.Current.User = Users.GetUser(2100);

                try
                {
                    User csUser = new User();
                    csUser.Username = cpUser.CPUserName;
                    csUser.EnableDisplayName = true;
                    csUser.Profile.CommonName = cpUser.Username;
                    csUser.Password = "pa$$word";
                    csUser.Email = cpUser.CPEmail;

                    // Set TC specific properties required to create user
                    csUser.AccountStatus = UserAccountStatus.Approved;
                    csUser.IsAnonymous = false;

                    CreateUserStatus status = CreateNewUser(csUser);

                    if (status == CreateUserStatus.Created)
                    {

                       IRoleService roleService = Services.Get<IRoleService>();      

                       // Add standard OOTB TC roles 'Everyone' and 'Registered User'
                       Roles.AddUserToRole(cpUser.Username, "Everyone");
                       Roles.AddUserToRole(cpUser.Username, "Registered Users");


                        // Switch context back to current user
                        CSContext.Current.User = cpUser;

                        return csUser;
                    }

                    // Couldn't create user
                }
                catch
                {
                    // User not created
                   
                    // Switch context back to current user
                    CSContext.Current.User = cpUser;
                   
                    return null;
                }

                // Switch context back to current user
                CSContext.Current.User = cpUser;

                return null;
            }

            /// <summary>
            /// Creates the new user, returning the user creation status.
            /// </summary>
            /// <param name="user">User</param>
            /// <returns>CreateUserStatus</returns>
            public static CreateUserStatus CreateNewUser(User user)
            {
                // Attempt to create the user
                if (user == null || user.Username == "Anonymous")
                {
                    return CreateUserStatus.DuplicateUsername;
                }

                try
                {
                    return Users.Create(user, true);
                }
                catch (CSException exception)
                {
                    return exception.CreateUserStatus;
                }
                catch (Exception ex)
                {
                    return MemberRoleProfileProvider.Instance().Members.GetCreateUserStatus(ex);
                }
            }


    Thanks for your help.


    Adam

  • Hi Alex, I don't know if you have had chance to test my code, but If not would you be able to provide the most basic code required to create a user in tc5 and set all the required permissions to emulate registering a user using the standard createuser form?

    Thanks

    Adam

  • I haven't yet had a chance to test your code.

    The recommended approach is to create users via the REST API - telligent.com/.../users.aspx .  There is a sample Create User application in the Platform SDK which should help you here.

  • Hi Alex, I have spent all day looking at this and still can't seem to find a resolution.

     

    have tried the following:

    1) created 2 users using the REST API V2 - get the same access denied error when trying to access the conversations page

    2) created 2 users using the internal ip (using the OOTB code from the dev SDK for the control panel & create user forms) - get the same access denied error when trying to access the conversations page

    3) created two users in the control panel - get the same access denied error when trying to access the conversations page

    4) ensured:  Administration » Site Administration » Setup > enable conversations = true

    5) ensured user > site settings > enable conversations with = all users

    I can not find any permissions, roles or any other settings in the control panel which control conversations.

    What else can I do to debug this Alex?

    Is there a way to track the CSException / Security Service / CSModule that handles this and see what is throwing the 31 error code?

    Thanks

    Adam

     

     

  • Can you remove the CSException CSModule from your communityserver.config file.  You should then get an ASP.Net stack trace instead of a friendly error.  Can you post that stack trace.

    (N.B. make sure to restore the CSException module before going into production).

  • Hi Alex,

    I have done this, please see below. I have Googled to see if I could see what the error ment but don't seem to find anything useful. Any ideas?

    Its very strange as the conversation starter never has this issue and can see the conversations page fine, only the user who recieved the conversation gets the error:

    Server Error in '/' Application.

    --------------------------------------------------------------------------------

    Unhandled Execution Error

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: CommunityServer.Components.CSException:

    Source Error:

    An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.  

    Stack Trace:

    [CSException]

      CommunityServer.Messages.Messaging.GetConversation(Guid conversationId) +255

      CommunityServer.Messages.Controls.ConversationMessageList.get_DataSource() +466

      System.Web.UI.WebControls.Repeater.ConnectToDataSourceView() +183

      System.Web.UI.WebControls.Repeater.GetData() +13

      System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +220

      CommunityServer.Controls.WrappedRepeater.CreateControlHierarchy(Boolean useDataSource) +145

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.CreateControlHierarchy(Boolean useDataSource) +372

      System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51

      CommunityServer.Controls.WrappedRepeater.OnDataBinding(EventArgs e) +14

      System.Web.UI.WebControls.Repeater.DataBind() +75

      CommunityServer.Controls.WrappedRepeater.DataBind() +143

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.DataBind() +28

      System.Web.UI.Control.DataBindChildren() +211

      System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +102

      System.Web.UI.Control.DataBind() +15

      System.Web.UI.WebControls.Repeater.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem) +124

      System.Web.UI.WebControls.Repeater.CreateControlHierarchy(Boolean useDataSource) +443

      CommunityServer.Controls.WrappedRepeater.CreateControlHierarchy(Boolean useDataSource) +145

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.CreateControlHierarchy(Boolean useDataSource) +372

      System.Web.UI.WebControls.Repeater.OnDataBinding(EventArgs e) +51

      CommunityServer.Controls.WrappedRepeater.OnDataBinding(EventArgs e) +14

      System.Web.UI.WebControls.Repeater.DataBind() +75

      CommunityServer.Controls.WrappedRepeater.DataBind() +143

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.DataBind() +28

      CommunityServer.Controls.PreTemplatedWrappedRepeaterBase.OnLoad(EventArgs e) +32

      System.Web.UI.Control.LoadRecursive() +50

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Control.LoadRecursive() +141

      System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

    --------------------------------------------------------------------------------

    Version Information: Microsoft .NET Framework Version:2.0.50727.3053; ASP.NET Version:2.0.50727.3053

    Thanks

    Adam

  • This error is occuring because the current user is not a participant in the current conversation.

    Conversation conversation = Provider.GetConversation(conversationId);

    bool b = conversation.Participants.IsParticipant(CSContext.Current.User)

    The exception is occurring because that last line is returning false.  This means either CSContext.Current.User is null, or the conversation.Participants collection does not contain CSContext.Current.User.

    I know you have some code which temporarily changes CSContext.Current.User so make sure that isn't interfering with the method.  Otherwise who is listed on the conversation Participant collection?

  • Hi Alex,

    in the end it was indeed the context being changed that caused this issue. the context switching code which was used with the role service caused the conversations page to get confused and throw this error. It was a very difficult bug to track down but your response really helped point me in the right direction so thanks for that.

    Adam