Search code examples
asp.netpostgresqlroledevartisinrole

Why @User.IsInRole not working?


I'm using licensed Devart dotConnect product to connect to a PostgreSql database in my ASP.NET MVC5 project. I have problem with Devart Rolemanager. I installed Dotconnect PostgreSql with folowing this tutorial https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role

All works fine, but when I call admin role in View, this function not working properly.

In DB the table (aspnet_users) I've a User (Foo) and in the table (aspnet_roles) Admin role, in table (aspnet_usersinroles) there are connected with Id.
Function:

@if(User.IsInRole("admin"))
{
<p>Test Admin page!</p>
}

Quick watch showing : false.

Here is part of my web.config:

  <connectionStrings>

    <add name="DefaultConnection" connectionString="host=00.000.000.0;database=DB;user id=***;password=*******; Initial Schema=users" providerName="Devart.Data.PostgreSql"/>
    <add name="DbEntities" connectionString="metadata=res://*/ModelDbFirst.csdl|res://*/ModelDbFirst.ssdl|res://*/ModelDbFirst.msl;provider=Npgsql;provider connection string=&quot;PORT=5520;TIMEOUT=15;POOLING=True;MINPOOLSIZE=1;MAXPOOLSIZE=20;COMMANDTIMEOUT=20;COMPATIBLE=2.2.5.0;DATABASE=;HOST=46.167.233.7;USER ID=***;PASSWORD=****;" providerName="System.Data.EntityClient"/>
  </connectionStrings>
  <system.web>
    <authentication mode="Forms">
      <forms loginUrl="~/account/login" timeout="2880"/>
    </authentication>
    <customErrors mode="Off" defaultRedirect="~/account/error"/>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
    <membership defaultProvider="AspNetPgSqlMembershipProvider"
      userIsOnlineTimeWindow="15">
      <providers>
        <add name="AspNetPgSqlMembershipProvider" type="Devart.Data.PostgreSql.Web.Providers.PgSqlMembershipProvider,Devart.Data.PostgreSql.Web, Version=7.4.478.0, Culture=neutral, PublicKeyToken=09af7300eec23701" description="dotConnect for PostgreSQL extended membership provider" connectionStringName="DefaultConnection"
        enablePasswordRetrieval="false"
        enablePasswordReset="true"
        requiresQuestionAndAnswer="true"
        requiresUniqueEmail="false"
        passwordFormat="Hashed"
        maxInvalidPasswordAttempts="5"
        applicationName="/"
        passwordAttemptWindow="10" />
      </providers>
    </membership>
    <roleManager defaultProvider="AspNetPgSqlRoleProvider"
      enabled="true"
      cacheRolesInCookie="true"
      cookieName=".ASPROLES"
      cookieTimeout="30"
      cookiePath="/"
      cookieProtection="All" >
      <providers>
      <add name="AspNetPgSqlRoleProvider" type="Devart.Data.PostgreSql.Web.Providers.PgSqlRoleProvider, Devart.Data.PostgreSql.Web, Version=7.4.478.0, Culture=neutral, PublicKeyToken=09af7300eec23701" description="dotConnect for PostgreSQL extended role provider" applicationName="/" connectionStringName="DefaultConnection"/>
      </providers>
    </roleManager>
    <siteMap defaultProvider="PgSqlSiteMapProvider">
      <providers>
        <add name="PgSqlSiteMapProvider"
             type="Devart.Data.PostgreSql.Web.Providers.PgSqlSiteMapProvider,
                 Devart.Data.PostgreSql.Web, Version=7.4.478.0, Culture=neutral,
                 PublicKeyToken=09AF7300EEC23701"
             connectionStringName="ConnectionString"
             securityTrimmingEnabled="true"
           />
      </providers>
    </siteMap>
  </system.web>
  <system.webServer>
    <staticContent>
      <mimeMap fileExtension=".json" mimeType="application/json"/>
    </staticContent>
    <handlers>
      <remove name="ExtensionlessUrlHandler-Integrated-4.0"/>
      <remove name="OPTIONSVerbHandler"/>
      <remove name="TRACEVerbHandler"/>
      <add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0"/>
    </handlers>
  </system.webServer>
  <runtime>
    <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.WebPages.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
            </dependentAssembly>
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Razor" publicKeyToken="31BF3856AD364E35" culture="neutral"/>
                <bindingRedirect oldVersion="0.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
            </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="EnvDTE" publicKeyToken="B03F5F7F11D50A3A" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-8.0.0.0" newVersion="8.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Helpers" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.WebPages" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="3.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35"/>
        <bindingRedirect oldVersion="1.0.0.0-5.2.2.0" newVersion="5.2.2.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="WebGrease" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-1.6.5135.21930" newVersion="1.6.5135.21930"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-6.0.0.0" newVersion="6.0.0.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Antlr3.Runtime" publicKeyToken="eb42632606e9261f" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.5.0.2" newVersion="3.5.0.2"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
      </dependentAssembly>
      <dependentAssembly>
        <assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0"/>
      </dependentAssembly>
    </assemblyBinding>
  </runtime>
  <entityFramework>
    <defaultConnectionFactory type="Npgsql.NpgsqlFactory, Npgsql">
      <parameters>
        <parameter value="v12.0"/>
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="Devart.Data.PostgreSql" type="Devart.Data.PostgreSql.Entity.PgSqlEntityProviderServices, Devart.Data.PostgreSql.Entity, Version=7.4.478.6, Culture=neutral, PublicKeyToken=09af7300eec23701"/>
      <provider invariantName="Npgsql" type="Npgsql.NpgsqlServices, Npgsql.EntityFramework"/>
    </providers>
  </entityFramework>
  <system.data>
    <DbProviderFactories>
      <remove invariant="Devart.Data.PostgreSql"/>
      <add name="dotConnect for PostgreSQL" invariant="Devart.Data.PostgreSql" description="Devart dotConnect for PostgreSQL" type="Devart.Data.PostgreSql.PgSqlProviderFactory, Devart.Data.PostgreSql, Version=7.4.478.0, Culture=neutral, PublicKeyToken=09af7300eec23701"/>
    </DbProviderFactories>
  </system.data>
</configuration>

How can I fix it ?


Solution

  • not sure if it is a typo in the query or the problem with your view, but the code snipped (which is very abbreviated) seems to contain an error. You don't seem to have an if statement there.

    @User.IsInRole("admin") { }
    

    should be

    @if (User.IsInRole("admin")) { }