Membership Roles in Azure and MVC - asp.net-mvc-3

I'm developing an ASP.NET MVC3 website and I'm using the Microsoft ASP.NET Universal Providers.
Web.config :
<connectionStrings>
<add name="MyContext" connectionString="data source=tcp:***.database.windows.net,1433;Initial Catalog=***;User Id=***;Password=***;Trusted_Connection=False;Encrypt=True;Connection Timeout=30" providerName="System.Data.SqlClient" />
</connectionStrings>
...
<roleManager defaultProvider="DefaultRoleProvider" enabled="true">
<providers>
<clear />
<add name="DefaultRoleProvider" type="System.Web.Providers.DefaultRoleProvider" connectionStringName="MyContext" applicationName="/" />
</providers>
</roleManager>
The connectionStrings is ok in Azure.
Aspnet tables and storedprocedures I added by using the query script. Data in tables added by migrations. I checked it in management portal.
I can get profile membership and profile data, but the the code below returns empty list.
var userName = User.Identity.Name;
var userRoles = Roles.GetRolesForUser(userName).ToList();
userName is correct and table aspnet_UsersInRoles contains correct data too.
There is no exceptions on page.
Why the userRoles is empty?

I fix this problem by creating the custom provider, which is inherited from SqlRoleProvider.

Related

How to create admin user in mvc3?

I want to add user registration to my site and I will need to create admin, who manages users. I am using authentication of ASP.NET. I can create user with Account/Register, listing them with Membership.GetAllUsers() on my admin panel and delete with Membership.DeleteUser( username )
I need help about admin. How can I create 1 admin and make sure nobody can be admin, be only simple user?
And what is role of user in Membership.CreateUser( model.UserName, model.Password, model.Email, null, null, true, null, out createStatus ) method of Account/Register?
For MVC account management I have found the MembershipStarterKit to be very useful. It sounds like you want Role based authentication which it supports very well.
The Membership.CreateUser function that you are referring to is not related to admin or role based security and is used only to create the user itself.
You can use the IIS .NET Users tool but because that targets .NET 2.0 you need to do some config around that to make it work. You need to create a new IIS application that uses the same connection strings as the real application.
That application needs a webconfig that looks like
<configuration>
<connectionStrings>
<add name="MyConnectionString" connectionString="same as my production application membership connection string" providerName="same as my production application provider" />
</connectionStrings>
<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<membership>
<providers>
<remove name="AspNetSqlMembershipProvider" />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, EnablePasswordReset=true" connectionStringName="MyConnectionString" applicationName="/" />
</providers>
</membership>
<roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="MyConnectionString" applicationName="/" />
</providers>
</roleManager>
</system.web>
</configuration>
When you add the application with this webconfig structure you need to make sure that you use an app pool that uses .NET v 2.0 integrated pipeline (DefaultAppPool should work).
One this is done, within this mini application you will have access to the .Net users feature where you can add and edit users and roles. However, this is not as useful a tool as the MembershipStarterKit.

Calling a Membership database Stored Proc directly from within an MVC web application

I have an MVC 3 web application using Entity Framework to access the projects database. However, the project also uses a second database for Memberships called ApplicationServices. The MvcMembership package uses this database for authentication purposes. Now, I added a custom stored procedure in the ApplicationServices database that I would like to call from my application. How can I do that?
I guess I could do so by using the Membership Provider, but I am not sure how to do so.
Here is how it is defined in my Web.config file:
<add name="ApplicationServices" connectionString="Data Source=.\SQLEXPRESS;Persist Security Info=false; Initial Catalog=ApplicationServices; User ID=******; Password=******" />
...
<membership defaultProvider="AspNetSqlMembershipProvider">
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" ... applicationName="/" />
</providers>
</membership>
No you can't call it from the default SqlMembershipProvider because this is a custom Stored Procedure and the Membership Provider know nothing about it.
Anyway you still could access this stored procedure like normal. Or you may make custom MembershipProvider and use this StoredProcedure in it. It depends on what and why you use this StoredProcedure

Multiple Membership Provider in web.config

I've a WCF web the config file which requires multiple membership provider due to reason below:
1. One membership for WCF which uses application A.
e.g.
<add name="MySqlMembershipProvider1"
connectionStringName="ApplicationServices"
applicationName="ApplicationA"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
type="System.Web.Security.SqlMembershipProvider" />
The other membership for creating and updating asp.net users.
<add name="MySqlMembershipProvider2"
connectionStringName="ApplicationServices"
applicationName="ApplicationB"
requiresQuestionAndAnswer="false"
requiresUniqueEmail="true"
type="System.Web.Security.SqlMembershipProvider"
/>
How do I specify the provider name on above scenario.
SqlMembershipProvider p1 = SqlMembershipProvider)
Membership.Providers["MySqlMembershipProvider1"];
....//
then using the membership providers's instance p1 for Application you can do whatever you need
SqlMembershipProvider p2 = (SqlMembershipProvider)
Membership.Providers["MySqlMembershipProvider2"];
....//
please see this link

Newbie in MVC3 connection setup

I'm a real newbie in MVC3 but excited to learn. Guide me how to change connection from App_Data (DB.mdf) to MS SQL Server 2008.
MDF connection
<add name="MvcMusicStoreEntities" connectionString="metadata=res://*/Models.MusicStore.csdl|res://*/Models.MusicStore.ssdl|res://*/Models.MusicStore.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\MvcMusicStore.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
This is a working one from my running project:
<add name="CONNECTION_NAME" connectionString="data source=SQL_SERVER_IP\INSTANCE_NAME; User ID=SQL_USER_NAME; Password=YOUR_PASSWORDd;Initial catalog=DB_NAME" providerName="System.Data.SqlClient" />
for your posted one, it may look like this:
as I can see, you are not following the MVC3 version of music store, or that's how it look for me as I've checked the PDF file of the MVC3 vesion and found that Jon Galloway is using the connecion string just like the one I'm using.
check out the PDF from here: http://mvcmusicstore.codeplex.com/releases/view/59112#DownloadId=197609
That's it, no other parts are required.
this is also a very very nice resource for you to find the different ways that you can do to configure your app connection string.
http://www.connectionstrings.com/sql-server-2008
From SQL Server 2008 connection strings
Data Source=myServerAddress;
Initial Catalog=myDataBase;
User Id=myUsername;
Password=myPassword;
Example:
<add name="NAME" connectionString="Data Source=.\SQLExpress; User ID=username; Password=password;Initial catalog=database" providerName="System.Data.SqlClient" />
If you are going to use Entity Framework you need to change the provider to
providerName="System.Data.EntityClient"
And modify the add tag:
<add name="Entities" connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=.\SQLExpress;Initial Catalog=Test;Persist Security Info=True;User ID=test;Password=test;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient"/>

Where did aspnetdb.mdf come from in my ASP.NET MVC Application?

I'm new to ASP.NET and have just starting to learn ASP.NET MVC 3. I've started a new ASP.NET MVC 3 project using the default template, which already has most of the membership stuff already configured. Now I'm trying to understand what is really going on behind the scenes.
I've found that default template defines an IMembershipService interface which is implemented by the AccountMembershipService class which basically just wraps a System.Web.Security.MembershipProvider. From the comments in the code is sounds like this abstraction is done to facilitate unit testing.
The default MembershipProvider is set in the Web.config file. My project, which was created from the default template, it is set to the SqlMembershipProvider.
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
This references a connection string in the Web.config file, which references a SQL Express database "aspnetdb.mdf".
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
When I first created my project there was no aspnet.mdf file. But after running my app and registering a new user this file was automatically generated tables and all. What's going on here? What creates this file and specified the tables that should be created?
These are created by the SqlMembershipProvider class. If you'd like, you can customize your config file to use a different membership provider or to tell it what existing database to use.
Use this tool to add the appropriate tables, etc to your existing SQL Server installation: http://msdn.microsoft.com/en-us/library/ms229862(v=VS.100).aspx

Resources