Where are my Roles being stored? - asp.net

I've enabled Roles in my ASP.NET web application, but I'm slightly confused as to where they are being stored. In my old project, each Role would appear in the ASP.NET Roles table, and I could see them. However, in my new project, I can't see any Roles in the database table. The strange thing is, I can still use them (and the default Roles/Membership API) and everything works fine. Any ideas why they are not appearing the database table itself, and where they are actually being stored?
My web.config file has this:
<roleManager enabled="true" />
Using the ASP.NET configuration tool, my default Role provider is AspNetSqlRoleProvider.
Thanks
Update
I added this explicitly in my web.confg
<roleManager enabled="true" >
<providers>
<clear/>
<add connectionStringName="myDBConnectionString" name="AspNetSqlRoleProvider"/>
</providers>
</roleManager>

By default, it does use the local database unless the role provider points to a database using a different database connection.
If you setup the tables in your own custom database and not the ASPNETDB.mdf file, then you will have to setup a custom provider definition pointing to the SQLroleprovider, and provide a custom connection string reference to where the database is.
HTH.

In your asp.net membership database are two tables: aspnet_Roles contains the roles and aspnet_UsersInRoles contains the association of users with roles.

it will build it’s OWN database in the App_Data folder and create the required tables there

Related

ASP.NET IDENTITY WEB FORMS

OK. I am experiencing something very strange with the asp.net identity in web forms. I setup the web.config file to establish connection to my local sqlexpress server. I previously created a database and ran the web application which uses asp.net identity and the 4.5 .net framework. The first time I ran the web app the tables were automatically created for me in the sql server database but then I noticed that in the APP_DATA folder under the solution there was another database created with a name aspnetdb.mdf
Although I have pointed out that the connection string connects to the sql server for some reason the web app connects to the newly created localdb database located in APP_DATA.
Am I doing something wrong here?
thanks
Oddly enough I have just come across this very issue in our codebase. One of my developers was using the 'old' Role provider to check if user was in a particular role. This was causing the app to create the database in App_Data as the config for all that still exists in machine.config. The connection string and provider details are specified in machine.config, so your web.config will only add or remove them. So for example if your machine.config says:
<connectionStrings>
<add name="LocalSqlServer" connectionString="..."/>
</connectionStrings>
And your web.config has this:
<connectionStrings>
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
Your application will be able to see and use both strings. To fix it you can clear the connection strings first like this:
<connectionStrings>
<clear />
<add name="MyConnectionString" connectionString="..."/>
</connectionStrings>
But that still leaves the underlying problem that the old role (or profile) provider is being used in code. so you will instead get lots of errors when using it. You need to switch code like this:
if (User.IsInRole("Admin"))
{
//Do admin stuff
}
To something like this (in my case _userManager is of type UserManager<User> and is injected into my controller constructor at runtime.
var user = _userManager.FindById(User.Identity.GetUserId());
if (_userManager.IsInRole(user.Id, "Admin"))
{
//Do admin stuff
}

simple membership provider in mvc

How to make simple Membership provider from empty web application template in ASP.NET MVC4
I searched a lot on google, bing and many others, but I din't get positive responce about membership provider
can some one tell me basic of membership provider?
please
I followed these steps:
So before starting I am assuming you have setup your database models including a users model which we will use for simple membership. Go ahead and add a "username" column and an "id" column (if you don't already have one) in the model and create the database. Remember already having a database is necessary if you want to use simple membership with your existing user's table. Simple membership will add it's table to your existing database.
1.Install Webmatrix.webdata and webmatrix.data from Nuget Packet manager.
2.In your web.config enable simple membership by
<add key="enableSimpleMembership" value="true" />
in appsettings
3.Next step is to define profile, role and membership providers in system.web by
<profile defaultProvider="SimpleProfileProvider">
<providers>
<add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="YOUR_CONNECTION_STRING" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager enabled="true">
<providers>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
4.Next step is to connect your user table with simple membership. Now the Internet Application Template which is being provided by default uses a filter but we're going to skip it. We're going to directly initialize simple membership in global.asax by
if (!WebSecurity.Initialized)
{
WebSecurity.InitializeDatabaseConnection("YOUR_DB_CONTEXT", "USER_TABLE", "ID_COLUMN", "USERNAME_COLUMN", true);
}
And you are done with the setup.
Now to actually create users, there are two options, Create User and Account and Create Account only. How the create user and account works is that it will you will provide the user's information and it will create a user in your user table and then create a membership account. I personally use the latter, which means I create the users separately, and then create a membership account for that user using
WebSecurity.CreateAccount("Username","Password");
Just create an Internet template, and copy the code out of it into your empty project.. although, honestly at that point you've essentially got the Internet template anyways, other than the default layout.
There's a lot of code that goes into supporting the Membership system, so study the Internet template and it will tell you everything you need to know.
When you create the new project, select the Internet Template.
When you register your first user it will automatically create the table structure in your db
So you want to use simple membership in asp.net MVC4 .
Follow the steps mentioned in this tutorial : Simple Membership
This will provide the all the basic information of how to setup simple membership in asp.net mvc4.

Adding a user in table to Role declare in asp.net

i have created a role in asp.net as :
<configuration>
<CustomUsersSection>
<Roles>
<add RoleName="Administrator"/>
</Roles>
<Users>
<add UserName="admin" Password="password"
Email="abot#home" Role="Administrator"/>
</Users>
</CustomUsersSection>
</configuration>
Now i want to add all the Admin that are added into a table from a sql-server to the role define above. Thanks for any assistance.
My Admin table structure is as follows:
AdminId
AdminName
EmailAddress
...
Assuming you're using ASP.NET membership, I would look into implementing your own membership provider. You can do it relatively easily and there is a lot of material on the Web about how to do that. You will then be able to implement your own test (such as whether a record exists in a database table) to determine whether a user is in the Admin role.
I suggest, though, that you learn about the built-in Membership Providers and the membership database ASP.NET automatically creates before you do this. This is because that's a pretty decent implementation and does things that would be labor-intensive to do for yourself, such as encrypting the information being stored to protect it from being easily hacked.

How to use ASP.NET Configuration on external SQL Server?

In visual studio there is a nice possibility to manage users and memberships for an ASP.NET site. I moved the membership data from SQLEXPRESS to a normal SQL server. The website works fine, however how do i now manage my users/profiles/etc... like I was used to in visual studio? Is it possible to tell vs2010 to 'look' into the new sql server database in stead of the apsnetdb.mdf file?
Thanks,
Erik
This is how I do it:
In your web.config file, under the connectionStrings section, name your connection string, LocalSqlServer, i.e.
<connectionStrings>
<remove name="LocalSqlServer" />
<add name="LocalSqlServer" connectionString="Server=123.123.123.123;Database=dbName;User ID=dbuser;Password=dbPassword;Trusted_Connection=False;" />
</connectionStrings>
Your role and membership providers, if have the connectionStringName set, change them to LocalSqlServer otherwise, the default providers "should" automatically refer to LocalSqlServer anyway.
In VS2010, I then simply go to Project > ASP.NET Configuration which will start the tool to manage users, roles etc.
HTH

ASP.NET Configuration Wizard to login with remote sql server

I have been learning ASP.NET by experimenting, watching videos on ASP.NET, and reading forums. I haven't found a quick solution to this though.
Whenever I add the "login" or "createuserwizard" from the toolbox it always adds the new users to a database known as "ASPNETDB.MDF" even if I specify the remote database using a new SqlDataSource.
Is there an easy way to save the login information? Any tutorials that helped you?
By default, ASP.NET will use a local file based database to store the login information.
If you want to use a different database, you need to do a couple of things:
Set up the Remote Database using aspnet_regsql.exe (usually found in C:\Windows\Microsoft.NET\Framework\v2.0.50727) - running this will start it in GUI mode, allowing you select a server and database to add the tables and stored procs to.
Configure your web site to use this database. There are a few of places you need to do this:
In the ConnectionStrings section of the web.config add your new ConnectionString:
<add
name="zhpCoreContentConnectionString"
connectionString="Data Source=Hobbiton\SqlExpress;Initial Catalog=zhpCoreContent;Integrated Security=True"
providerName="System.Data.SqlClient"/>
In the MembershipProvider section of the web.config, ensure the ConnectionString attribute is set to the same name as your connection string (other settings elided for berevity):
<membership>
<providers>
<add
connectionStringName="zhpCoreContentConnectionString"
applicationName="/doodle"
name="AspNetSqlMembershipProvider"
type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
</providers>
</membership>
If you are using Roles or Profiles you'll want to ensure that they are also using your connectionstring to store everything together, and ensure that the applicationName attribute is common between them.
This sounds like you need to Implement a Custom Membership user

Resources