Getting error “The user instance login flag is not

2019-08-27 00:52发布

I recently upgraded a SQL Server Express database I was using locally to SQL Server 2012 Developer. I started getting this error. A quick search pulls up tons of posts where the answer seems to be to remove User Instance=True; from your connection strings.

I checked and none of my connection strings have this flag. I tried searching the entire solution for "user instance" just to make double sure and it doesn't show up anywhere. Doubly weird is that I don't get this error right away. The first couple calls to the database work...

Has anyone else had a similar experience?

Here is the exact code that is causing my error:

public class BetterAuthorizeAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(System.Web.Mvc.AuthorizationContext filterContext)
    {
        var roleList = Roles.Split(',');

        //if the user is in one of the roles in the list
        foreach (string r in roleList)
        {
            if (filterContext.HttpContext.User.Identity.IsAuthenticated)
            {
                //The error is consistently thrown here the second time this line
                //executes...
                if (filterContext.HttpContext.User.IsInRole(r.Trim()))
                {

Here are the config sections I thought were relevant:

<connectionStrings>
  <add name="BioSphereContext" 
       providerName="System.Data.SqlClient" 
       connectionString="Server=fake;Database=fake;User ID=fake;Password=fake;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;MultipleActiveResultSets=true;" />
</connectionStrings>

.
.
.

<sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="15">
  <providers>
    <add name="DefaultSessionProvider" 
         type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
         connectionStringName="DefaultConnection" applicationName="/" />
  </providers>
</sessionState>


.
.
.

<entityFramework>
    <defaultConnectionFactory 
        type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
       <parameters>
          <parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; MultipleActiveResultSets=True" />
       </parameters>
    </defaultConnectionFactory>
</entityFramework>

Some more details about my project in case it helps...

  • VS 2012
  • Entity Framework 5
  • Azure SDK 2.0
  • ASP.NET MVC 3
  • .net 4.5

2条回答
Fickle 薄情
2楼-- · 2019-08-27 01:31

This code uses ASP.NET Membership Roles Provider

if (filterContext.HttpContext.User.IsInRole(r.Trim()))

The machine.config shows

            <roleManager>
            <providers>
                <add name="AspNetSqlRoleProvider" connectionStringName="LocalSqlServer" applicationName="/" type="System.Web.Security.SqlRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
                <add name="AspNetWindowsTokenRoleProvider" applicationName="/" type="System.Web.Security.WindowsTokenRoleProvider, System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
            </providers>
        </roleManager>

Pointing to "LocalSqlServer" in the same file

<connectionStrings>
    <add name="LocalSqlServer" connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true" providerName="System.Data.SqlClient"/>
</connectionStrings>

There is the User Instance.

TODO: Configure your roleManager to use either another roles provider or the AspNetSqlRoleProvider to use a different connection string.

same goes for session (What is DefaultConnection? i don't see a connection string named like this), membership and profile.

So try:

<sessionState mode="InProc" customProvider="DefaultSessionProvider" timeout="15">
  <providers>
    <add name="DefaultSessionProvider" 
         type="System.Web.Providers.DefaultSessionStateProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" 
         connectionStringName="BioSphereContext" applicationName="/" />
  </providers>
</sessionState>
查看更多
太酷不给撩
3楼-- · 2019-08-27 01:38

Not sure what was wrong, but I deleted and re-cloned my git repository. Error went away.... never getting that day back... lol. I'll leave the post in case any of this is helpful to others.

查看更多
登录 后发表回答