Umbraco 5 how to get all roles and users - umbraco

I want to get all the membership roles in the system and all the members in the system.
I've tried using
System.Web.Security.Roles.GetAllRoles();
System.Web.Security.Roles.GetUsersInRoles(roles[0]);
and a couple of others, but they all throw the not implemented error.
I'm using a clean VisualStudio Umbraco template of V5.1 running locally on IIS Express and SQL Express.
Any thoughts would be much appreciated.

I noted that the provider was not queried in populating the role listing from the backoffice and came to the conclusion that not only was getallroles not called, it was never implemented.
Instead, the hive is queried for the list of roles.
Despite the claim that membership services was baked-in again starting with 5.1, it comes with some particularly serious limitations.
If you're committed to 5.1 and you need a custom roleprovider, then you'll need to come up with a solution that occasionally syncs roles to Umbraco.
If you don't need a custom roleprovider, then you can query the hive to pull the related content. The special urls are:
security://profiles
Used to store profile data by the Hive Membership Wrapper. Profile data is additional data for any member/user that cannot be stored in the ASP.Net MembershipProvider
security://user-groups
Used to store the data for back office user groups/roles.
security://member-groups
Used to store the data for members' groups/roles.
security://member-types
Used to store the schema data for member types
security://membership-data
Used by the UmbracoMembershipProvider to store the ASP.Net MembershipProvider information
security://users
Used to query the Hive membership provider wrapper for back office users
security://members
Used to query the Hive membership provider wrapper for Umbraco members
Finally, be aware that membership services are now abstracted. There's a completely new separate interface for Umbraco's take on membership.
Rather than using the 'baseline' .net membership provider and role provider, you use the membership service available in your current IRoutableRequestContext:
e.g. rather than using Membership.ValidateUser(), you would use _context.Application.Security.Members.Validate() which wraps the supplied MembershipProvider.
Good luck, and post any findings of your own as the community trudges through this release together.
EDIT: An example for getting a list of the member roles
using (var securityUow = context.Application.Hive.OpenReader<ISecurityStore>())
{
return securityUow.Repositories.GetEntityByRelationType<UserGroup>
(
FixedRelationTypes.DefaultRelationType,
Umbraco.Framework.Security.Model.FixedHiveIds.MemberGroupVirtualRoot
).OrderBy(x => x.Name).ToList();
}

Related

How ASP.NET MVC stores user information?

I am new to MVC, Have created simple application (very basic, only 1 controller) & used Entity Framework as ORM.
There is form for Registration in application, when I fill up that form user is registered & able to login with those credentials.
But there is no table created in my Database, so my question is where this registration information is stored?
I know this need knowledge of May be Membership/forms authentication, but I don't know them also.
I tried to search google for this, but may be I am not able to predict what to search.
EDIT :
Following is tag of DefaultConnection
Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-AKShop-20140808100025.mdf
I think that standard DB was created for your membership. Probably at app_data\aspnetdb.mdf.
Before MVC 5, MembershipProvider or SimpleMembershipProvider by default, and data is persisted in (local) SQL server.
In MVC5, ASP.NET Identity 2.0 by default, and the data is persisted through Entity Framework which by default point to MS SQL, though it is easy to switch the persistence to other DB engines.
Identity 2.0 is quite new, and the official release is April this year, so this is why you got mixed info when searching the Internet. Nevertheless, if you have a green field MVC project, it might be better of to use MVC 5 and Identity 2.0.

MVC Simple Membership Web Site Administration Tool

I must have missed this somewhere (or slept through the class)... the standard ASP security tool for managing users and roles seems to be absent when using the new simple security version. That is, the original is there and working, just points to traditional ASP security... NOT to the new "simple" system.
In my project I can log in, I can look at the webpages_ tables but can find no method of accessing them in my project... I would be happy to manage roles and get the USERID and maybe ROLEID so I could tie the user to other functions in my project.
I've looked through hundreds of posts and articles... normally I find I am missing some minor config entry or something but this time I am a little more stuck.
Advice is appreciated.
Thanks!
SimpleMembership does not work with the Web Site Administration Tool (WSAT). There is a good overview of SimpleMembership in this blog. Here is a quote from it.
WSAT is built to work with ASP.NET Membership, and is not compatible with Simple Membership. There are two main options there:
Use the WebSecurity and OAuthWebSecurity API to manage the users and
roles.
Create a web admin using the above APIs.
Since SimpleMembership
runs on top of your database, you can update your users as you would
any other data - via EF or even in direct database edits (in
development, of course)
I would also add that you can seed some of this information at application start-up, such as what roles are available, as shown in this article.

asp.net mvc3, Roles from database

I am creating a asp.net mvc project which I want to manage the roles in the database.
I have a database with a table, in the table Called Premissions I have a column (AdministratorRole) that contains roles for my mvc project, the value will hold something like 'Domain\John Smith' (the users). I want my mvc project to check this column each time it starts up.
In my mvc project i am using the Authorize attribute in the controllers:
[Authorize(Roles = Roles.AdministratorRole)]
I was thinking of creating a Interface so I can use a IQueryable to query the database, and then add something in the Global.asax file in the application_start method so that it runs the interface first and check all roles. (How can I do this?)
This is so we can maintain the roles in the database rather than in the code of the project.
How can I do this please?
Thanks
ASP.Net uses a Provider model for Membership and Roles. If you are doing something custom, then you simply need to create a Custom RoleProvider.
You can implement a RoleProvider.
ASP.NET role management enables you to easily use a number of
different providers for your ASP.NET applications. You can use the
supplied profile providers that are included with the .NET Framework,
or you can implement your own provider.
There are two primary reasons for creating a custom role provider.
You need to store role information in a data source that is not
supported by the role providers included with the .NET Framework, such
as a FoxPro database, an Oracle database, or other data source.
You need to manage role information using a database schema that is
different from the database schema used by the providers that ship
with the .NET Framework. A common example of this would be
authorization data that already exists in a SQL Server database for a
company or Web site.

ASP Membership tables -> add new property to user?

I would like to start using ASP.NET's membership classes and tables with a new MVC4 project I am building.
Let's say for example I want to keep track of user's birthdays. When creating the account, how do I add in the birthday? Is this something I keep track of with Profiles? I'm a bit confused on the correct way to add new 'columns' of data for users?
To specifically answer your question, here's how Microsoft advises to create extra tables for storing additional user information: Storing Additional User Information
Here is another posting (I would take this approach), its implementing your own profile provider, rather than using default one, so you have full control over what is happening, how it stored etc.: Implementing Profile Provider in ASP.NET MVC
Another great article by Microsoft about the same is Manage Web Users With Custom Profile Providers
It totally depends on utility and use. You can either
use default profile provider (exercising)
use custom profile provider (small scale sites)
use your own tables to store user information (enterprise level).
In the latter case you can link between default membership (assuming you using default membership provider) and your custom profile information by including user.Guid inside your table, which is used by default membership as unique identifier.
Hope this information will help you.
Profiles is the right way although it has its disadvatages. The data in the database is not in a readable way but in special strings, and profile is loaded on every postback.
Look here:
http://msdn.microsoft.com/en-us/library/z1hkazw7(v=vs.100).aspx

ASP.NET MVC Patterns for Multiple Tenants and Plugins?

I am trying to understand how I can create an ASP.NET MVC site that exists as a VS2010 project in a solution, and then for multiple "tenants" I would create a site that inherits from that one. That would give the flexibility of adding modular features to one without affecting another one, and both could benefit from core library optimizations.
Is that a crazy idea? What patterns exist for that kind of thing? I have done something similar for a webform-based site (adding DLLS as plugins), but not in MVC.
A "tenant" is a business client. Each already has their own MSSQL database and seperate processing around them, each client is in its own silo. The databases are similar with a few features added here and there, they are versioned and deployed seperately, that whole process works well. A client has n logons. I want to develop a single "base site" that can then be used to give function to a tenant, and all activities are segerated for a tenant to a single database. Where things get ugly is how I can add a new component (say a forum) to one tenant site without mucking up the site experience for other tenants.
All ideas appreciated. Thanks.
I have worked extensively on the development of a multi-tenant web application. Here are three basic pointers to help you get started:
Security
The TenantId is part of the login credentials. These are stored in the Thread.CurrentPrincipal. This effectively binds each request (thread) to a specific user and thus tenant. Thread.CurrentPrincipal can be easily accessed from any code.
Database
We used a single database to store all data. A separation was made between tables (entities) that were specific for one tenant (multi-tenant), and tables that were not (cross-tenant). Tables that were multi-tenant had a column called 'TenantId'. In our entity model, we made sure these entities inherited from a special IMultiTenant interface. This interface contained the C# equivalent of the TenantId field. We extended the architecture of Entity Framework to provide default filtering on TenantId for multi-tenant entities. This ensured that one tenant could never access or modify the data of another tenant.
Plugins
We used a bit of Dependency Injection trickery in order to support the implementation of tenant specific code. Based on the current TenantId, our DI container injects a tenant-specific implementation of that interface.

Resources