Gerard
Gerard

Reputation: 2709

Error running MVC4 on Azure

We sometimes getting an error starting our MVC4 site on Azure. Never seen these errors on our local servers. After deploying the database and the application to Azure, troubles occur starting the homepage if authorization is requested and after a period of inactivity on the site (error likely caused by a timeout). The code is as simple as depicted below:

[InitializeSimpleMembership]
[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

Requesting the homepage sometimes fails:

[Win32Exception (0x80004005): Access is denied]

[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)]
....
System.Data.SqlClient.SqlConnection.Open() +96
System.Web.DataAccess.SqlConnectionHolder.Open(HttpContext context, Boolean revertImpersonate) +88
System.Web.DataAccess.SqlConnectionHelper.GetConnection(String connectionString, Boolean revertImpersonation) +239
System.Web.Security.SqlRoleProvider.GetRolesForUser(String username) +762
WebMatrix.WebData.SimpleRoleProvider.GetRolesForUser(String username) +54
...

How to prevent this (timeout) error? Thanks for any help!

Upvotes: 1

Views: 1398

Answers (2)

Gerard
Gerard

Reputation: 2709

Move the following line from InitializeSimpleMembershipAttribute.cs (a Filter) to AuthConfig.cs:

WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);

So the code will look like:

public static class AuthConfig
{
    public static void RegisterAuth()
    {
        WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
        ...

Perhaps you need the following in your web.config (section system.web) as well:

<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
 <providers>
  <clear/>
  <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData"/>
 </providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
 <providers>
  <clear/>
  <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
 </providers>
</membership>

Upvotes: 7

Neil
Neil

Reputation: 159

What membership system are you using and has it been initialised? If you are using SimpleMembership try:

[Authorize(Roles = "Administrator")]
[InitializeSimpleMembership]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

More information on this here Role based authentication in the new MVC 4 Internet template using simplemembership

Upvotes: 0

Related Questions