Error running MVC4 on Azure

强颜欢笑 提交于 2019-12-11 13:11:12

问题


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!


回答1:


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>



回答2:


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



来源:https://stackoverflow.com/questions/16708202/error-running-mvc4-on-azure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!