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.
Related
I am trying to convert my vb.net 2010 web forms application into a C# MVC 4 application. Since my membership is already setup, I would prefer to just continue using the membership provider instead of the simplemembershipprovider.
Hi,
I am using PluralSight to learn MVC, but have not been able to find any reference on how to convert membership code to mvc.
I have a custom table that is linked by UserID to the mememberships User table. This custom table, asp_customUser, stores additional information about the user.
In the account controller, there is a register action, with the view and model. The register process does not handle roles, much less my custom data.
What I would like to know is, how do I add roles and custom user information to the database, when part of the fields in the registration view come from the membership library and the other part go to my custom table.
Thanks
Yes, you can use the old membership. Just copy and paste your membership provider from the old web.config to the new one.
If this is running on a different computer, you may need to set the machine key, however, in order for it to be able to correctly validate the password hashes.
You would then remove the Simple membership initialization attribute from the Account controller, and you would need to remove all the other simple membership code. You would also be giving up the OpenId authentication, because normal membership does not work with WebSecurity, so you would have to remove all that as well.
When we wanted to extend the membership class we inherited the base membership class and used all the fields in it and the tables linked to it and then create an additional database table with a FK linked to the id in the membership table. Any additional parameters where in the new table and extended class and any standard parameters were in the std membership table and standard class.
I try to use OpenId in ASP.NET MVC4, SO I use This Tutorial, And as you see in Database Structure we need some additional tables to support OpenId and OpenAuth, as usual I use
C:\Windows\Microsoft.NET\Framework64\aspnet_regsql.exe
to implement Membership Database,
I install Visual Studio 2012 with .NETFramework 4.5 and MVC 4 so I run the aspnet_regsql.exe but the created database is not contain additional tables to support OpenId, So is there another way to implement this Database? Or I should add additional database by my own? and if yes how implement needed membership structure in Code side?
I think there must be an auto way (like before) to implement membership to support new OpenId feature, what is your suggestion?
By default, MVC 4 does not use the old membership tables, but rather a new membership system called SimpleMembership that has a very different table structure. You don't use aspnet_regsql, but rather let SimpleMembership create the tables automatically.
If you generate a default internet application with MVC 4, it creates all the code necessary to enable openid and openauth. This is built into the default sample. The example you linked to is for the Universal providers. They too, by the way, have a different table structure than the old SqlMembership system.
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();
}
I'm building an ASPNET MVC3 app; I have built, on the SQLServer DB, my users, roles and user_roles tables andI would like to use them instead of the aspnet_* tables built by using aspnet_regsql.exe.
Do you know if is this possible and, if so, in which way I can authenticate my users towards my tables?
You just need to implement your own Membership and Role providers.
see How do I create a custom membership provider for ASP.NET MVC 2?
Yes, you will need to build a custom memberShip provider
For some reason, the idea of setting up Membership in ASP.NET MVC seems really confusing.
Can anyone provide some clear steps to setup the requisite tables, controllers, classes, etc needed to have a working Membership provider?
I know that the Demo that MVC ships with has an Accounts controller. However, should I be using this in my own project? What do I need to get my existing database ready if so? If not, how do I learn what I need to do to implement a membership provider?
Check out this step by step blog on how to set up Membership provider in your asp.net mvc project. The sdk tool you need to get your database ready is aspnet_regsql.exe, you don't need to create a separate database to do that ( a lot of people think they have to provide a separate aspnet.db), you can run the command on your existing database, and it will create the tables, views, and stored procedures to handle the membership provider for you.
However, should I be using this in my
own project? What do I need to get my
existing database ready if so? If not,
how do I learn what I need to do to
implement a membership provider?
The benefit to use the default provider (SqlMembership provider) is to save yourself a lot of time. It involves a lot of work to design a complete membership and role provider.
Edit [2014-06-19] Asp.Net Identity Framework is Microsoft new recommendation to manage user sand permissions.
Check out this link:
https://github.com/TroyGoode/MembershipStarterKit
Most of the work is already done for you. Just download the sample project and run the aspnet_regsql.exe against your database.
check out my answer in this post;
membership
If you want to keep the membership provider that .Net creates for you then you can copy all the tables etc to another sql database and point the provider at it via the config file.
Post a comment if you need more than this.