Default role for authenticated users?

Topics: Business Logic Layer
Nov 2, 2011 at 3:51 PM

Hi,

I have set up BlogEngine.net 2.5  for use as an internal tool on our domain.

Basically, I want the following rules:

- If you are Authenticated, you are an Editor (Everyone who logs in, can contribute)

- If you are not Authenticated, you get to read the blog (for rss-feeds for info-screens etc).

I really would like not to have to manually add rows to the database, or to an xml file every time someone starts at the company.

 

 

I've set up an ActiveDirectoryMembershipProvider, and can successfully log in using any AD account.

Problem is, the user doesn't have any roles, and can not see any posts.

 

I've tried setting the Self-Registration role to "Editors", with no luck.

If I go to the Users admin page, I don't see any users at all, but when I look at the dashboard, I can see that I have ~100 users (which probably is correct).

If I enable "Allow self-registration", and try to register with an AD username  it (correctly) detects that this is a duplicate, and won't allow it to go through.

 

So, what I'd like to ask is:

- Is something wrong on my end, or is this expected behaviour?

Nov 2, 2011 at 4:32 PM

I just noticed, that the reason the users aren't listed, is because the /GetUsers json call is failing with an HTTP error 500.

I've enabled error logging, but for the life of me, I can't figure out where the file it logs to is.

 

I found a file called logger.txt  in the  App_Data folder, but it only contains a couple of errors I got when installing a new theme.

 

The exact message returned from the GetUsers call is:

  1. Request URL:
    http://<myserver>/admin/Users/Users.aspx/GetUsers
  2. Request Method:
    POST
  3. Status Code:
    500 Internal Server Error
  4. Request Headersview source
    1. Accept:
      application/json, text/javascript, */*; q=0.01
    2. Accept-Charset:
      ISO-8859-1,utf-8;q=0.7,*;q=0.3
    3. Accept-Encoding:
      gzip,deflate,sdch
    4. Accept-Language:
      da-DK,da;q=0.8,en-US;q=0.6,en;q=0.4
    5. Connection:
      keep-alive
    6. Content-Length:
      3
    7. Content-Type:
      application/json; charset=UTF-8
    8. Cookie:
      __utma=47152162.315458417.1285153421.1285153421.1285177229.2; rating=f50d9b47-ed25-4925-a03a-3dab5fd45b28; ToggleDisabledExtensions=Hide; .AUXBLOGENGINE-27604f05-86ad-47ef-9e05-950bb762570c=0A2DE291FF4D462C1C0DD16B1A4AE9C68879657628A4A2C997A58DAAD793B82ADBB68494497CB5F7972F36FE65D1ECBC520EF4394570C7082B03816ED0D9C52CA4B89B08340BFF2C9F33E5CB9F311E7C4DF9F4360C153C059FDEBE870C69F7AF65207E9A3E4D33FC7D9470F31DBD761795748542EEC59EEE4FABA769618A1F02A96A579C79E6DB0EF004759179340AF3
    9. Host:
      <myserver>
    10. Origin:
      <myserver>
    11. Referer:
      http://<myserver>/admin/Users/Users.aspx
    12. User-Agent:
      Mozilla/5.0 (Windows NT 5.1) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.782.220 Safari/535.1
    13. X-Blog-Instance:
      27604f05-86ad-47ef-9e05-950bb762570c
    14. X-Requested-With:
      XMLHttpRequest
  5. Request Payload
    1. { }
  6. Response Headersview source
    1. Content-Length:
      91
    2. Content-Type:
      application/json; charset=utf-8
    3. Date:
      Wed, 02 Nov 2011 15:09:13 GMT
    4. Server:
      Microsoft-IIS/7.5
    5. X-Powered-By:
      ASP.NET
    6. jsonerror:
      true

{"Message":"There was an error processing the request.","StackTrace":"","ExceptionType":""}-

Nov 2, 2011 at 4:56 PM

When I run from the server itself, it actually shows me whats going on.

 

{"Message":"The property \u0027LastLoginDate\u0027 is not supported by the Active Directory membership provider.","StackTrace":"   at System.Web.Security.ActiveDirectoryMembershipUser.get_LastLoginDate()","ExceptionType":"System.NotSupportedException"}

 

It probably comes from this code in admin\Users\users.aspx.cs

        [WebMethod]
        public static List<MembershipUser> GetUsers()
        {
            CheckSecurity();

            int count;
            var userCollection = Membership.Provider.GetAllUsers(0, 999, out count);
            var users = userCollection.Cast<MembershipUser>().ToList();

            users.Sort((u1, u2) => string.Compare(u1.UserName, u2.UserName));

            return users;
        }

 

Seeing as how it simply returns the List, the builitin serializer will start iterating over all public properties, including the LastLoginDate.

 

Not sure if I should fix it by wrapping the MembershipUser objects in another Custom membership user object, or if there is a better approach.

 

Nov 2, 2011 at 5:24 PM

Fixed it partially, by adding this inner-class to Users.aspx.cs

 

    public class MyMembershipUser {
        MembershipUser u;
        public MyMembershipUser(MembershipUser m)
        {
            u=m;
        }


        public string UserName { get { return u.UserName; }}
    }

 

And changed the function to do

       [WebMethod]
        public static List<MyMembershipUser> GetUsers()
        {
            CheckSecurity();

            int count;
            var userCollection = Membership.Provider.GetAllUsers(0, 999, out count);
            var users = userCollection.Cast<MembershipUser>().ToList();
            users.Sort((u1, u2) => string.Compare(u1.UserName, u2.UserName));

            List<MyMembershipUser> list = new List<MyMembershipUser>();
        foreach(var u in users)
            {
                list.Add(new MyMembershipUser(u));
             }
       
            return list;
        }

 

 

Granted, its not perfect, but now I can see my users, and change their profiles to set the editors role, which solves my original problem.

 

Could probably have been done better with some customer jsonSerializer, but when all you have is notepad via remote desktop, complexity isn't your friend :)