为什么@User.IsInRole 不起作用?
Why @User.IsInRole not working?
我正在使用获得许可的 Devart dotConnect 产品连接到我的 ASP.NET MVC5 项目中的 PostgreSql 数据库。我对 Devart Rolmanager 有疑问。
我按照本教程安装了 Dotconnect PostgreSql https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role
一切正常,但当我在视图中调用管理员角色时,此功能无法正常工作。
在数据库中 table (aspnet_users) 我有一个用户 (Foo) 并且在 table (aspnet_roles) 管理员角色中,在 table (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="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>
我该如何解决?
不确定是查询中的拼写错误还是您的视图有问题,但截取的代码(非常简短)似乎包含错误。您那里似乎没有 if 语句。
@User.IsInRole("admin") { }
应该是
@if (User.IsInRole("admin")) { }
我正在使用获得许可的 Devart dotConnect 产品连接到我的 ASP.NET MVC5 项目中的 PostgreSql 数据库。我对 Devart Rolmanager 有疑问。 我按照本教程安装了 Dotconnect PostgreSql https://www.devart.com/dotconnect/postgresql/articles/aspproviders.html#role
一切正常,但当我在视图中调用管理员角色时,此功能无法正常工作。
在数据库中 table (aspnet_users) 我有一个用户 (Foo) 并且在 table (aspnet_roles) 管理员角色中,在 table (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="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>
我该如何解决?
不确定是查询中的拼写错误还是您的视图有问题,但截取的代码(非常简短)似乎包含错误。您那里似乎没有 if 语句。
@User.IsInRole("admin") { }
应该是
@if (User.IsInRole("admin")) { }