为什么@ User.IsInRole不工作?

时间:2015-08-13 12:56:59

标签: asp.net postgresql role devart isinrole

我使用许可的Devart dotConnect产品连接到我的ASP.NET MVC5项目中的PostgreSql数据库。我有Devart Rolemanager的问题。 我安装了Dotconnect PostgreSql,并附上了本教程https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role

一切正常,但是当我在View中调用admin角色时,此功能无法正常工作。

在DB表中(aspnet_users)我是用户(Foo)和表(aspnet_roles)管理员角色,在表(aspnet_usersinroles)中有与Id连接。
功能:

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

快速观看显示:错误。

以下是我的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>

我该如何解决?

1 个答案:

答案 0 :(得分:1)

不确定它是查询中的拼写错误还是查看问题,但是剪切的代码(非常简略)似乎包含错误。你似乎没有if语句。

SET @dbname = DATABASE();
SET @tablename = "tableName";
SET @columnname = "colName";
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      (table_name = @tablename)
      AND (table_schema = @dbname)
      AND (column_name = @columnname)
  ) > 0,
  "SELECT 1",
  CONCAT("ALTER TABLE ", @tablename, " ADD ", @columnname, " INT(11);")
));
PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

应该是

one.Range.Text = "Test"