簡體   English   中英

User.IsInRole在服務器上不起作用

[英]User.IsInRole is not working on server

我的網站使用的是ASP.NET 2012,SQL Server 2008和C#。 當我執行以下代碼時:

if(User.IsInRole("Admin"))
    Response.Redirect("test.aspx");

Default.aspx.cs ,在Visual Studio中本地運行時沒有錯誤。 部署我的網站並嘗試以具有Admin Role的用戶登錄后,服務器上出現以下錯誤:

建立與SQL Server的連接時發生與網絡相關或特定於實例的錯誤。 服務器未找到或無法訪問。 驗證實例名稱正確,並且已將SQL Server配置為允許遠程連接。 (提供者:SQL網絡接口,錯誤:26-指定服務器/實例時出錯)

我的ASP.NET成員資格數據庫位於主機上。 我應該在Default.aspx.cs使用連接字符串嗎? 如果是,我該如何寫?

我確定與我一起登錄的User位於“ Admin Role

web.config

<configuration>
  <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>
  <connectionStrings>
    <add name="MembersConnectionString" 
         connectionString="workstation id=mssql;packet size=4096;user id=shahi_SQLLogin_1;pwd=password;data source=mssql;persist security info=False;initial catalog=akhbarrr" />
    <add name="SabteAKhabar" 
         connectionString="workstation id=mssql;packet size=4096;user id=shahi_SQLLogin_1;pwd=password;data source=mssql;persist security info=False;initial catalog=akhbarrr" />
    <add name="ConnectionString" 
         connectionString="workstation id=mssql;packet size=4096;user id=shahi_SQLLogin_1;pwd=password;data source=mssql;persist security info=False;initial catalog=akhbarrr" />
    <add name="akhbarrConnectionString" 
         connectionString="workstation id=mssql;packet size=4096;user id=shahi_SQLLogin_1;pwd=password;data source=mssql;persist security info=False;initial catalog=akhbarrr"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
  <system.web>
    <customErrors mode="Off">
      <error statusCode="403" redirect="access.aspx"/>
      <error statusCode="404" redirect="nofile.aspx"/>
    </customErrors>
    <anonymousIdentification enabled="true"/>
    <profile enabled="true" defaultProvider="AspNetSqlProfileProvider">
      <properties>
        <add name="FirstName" allowAnonymous="false"/>
        <add name="LastName" allowAnonymous="false"/>
        <add name="AkharinMadrak" allowAnonymous="false"/>
        <add name="Reshte" allowAnonymous="false"/>
        <add name="Gerayesh" allowAnonymous="false"/>
        <add name="NahveAshnayi" allowAnonymous="false"/>
        <add name="Address" allowAnonymous="false"/>
        <add name="PostalKod" allowAnonymous="false"/>
        <add name="City" allowAnonymous="false"/>
        <add name="Town" allowAnonymous="false"/>
        <add name="Number" allowAnonymous="false"/>
        <add name="BirthDay" type="System.DateTime" allowAnonymous="false"/>
      </properties>
    </profile>
    <authorization>
      <allow roles="Managers"/>
      <allow roles="Admin"/>
      <allow roles="AdminAsli"/>
    </authorization>
    <authentication mode="Forms">
      <forms timeout="5" cookieless="AutoDetect" protection="All"/>
    </authentication>
    <membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
      <providers>
        <clear/>
        <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="MembersConnectionString" applicationName="MyApplication" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed"/>
      </providers>
    </membership>
    <roleManager enabled="true"/>
    <pages validateRequest="false" enableEventValidation="false" viewStateEncryptionMode="Never" enableViewStateMac="false"/>
    <compilation debug="true" targetFramework="4.0">
      <assemblies>
        <add assembly="System.Web.Extensions.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
        <add assembly="System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A"/>
        <add assembly="System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089"/>
      </assemblies>
    </compilation>
    <httpRuntime/>
    <machineKey validationKey="1234567890123456789012345678901234567890AAAAAAAAAA" decryptionKey="123456789012345678901234567890123456789012345678" validation="SHA1" decryption="Auto"/>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add name="MSCaptcha" path="CaptchaImage.axd" verb="GET" type="MSCaptcha.CaptchaImageHandler, MSCaptcha" preCondition="integratedMode,runtimeVersionv2.0"/>
    </handlers>
  </system.webServer>
  <system.net>
    <mailSettings>
      <smtp from="xxxxxx@gmail.com">
        <network host="smtp.gmail.com" password="xxxxxx" port="587" userName="farzanehsd@gmail.com" enableSsl="true"/>
      </smtp>
    </mailSettings>
  </system.net>
</configuration>

您的C#代碼沒有錯誤,因為錯誤消息清楚地告訴您。

連接到數據庫時出錯,這很可能是由於配置文件中的條目丟失或不正確引起的。

樣本配置:

<connectionStrings>
    <add name="MembersConnectionString" 
        connectionString="Data Source=akhbarrr.mssql.somee.com;packet size=4096;User Id=YourUserID;Password=YourPassword;persist security info=False;Initial Catalog=akhbarrr" />
    <add name="SabteAKhabar" 
        connectionString="Data Source=akhbarrr.mssql.somee.com;packet size=4096;User Id=YourUserID;Password=YourPassword;persist security info=False;Initial Catalog=akhbarrr" />
    <add name="ConnectionString" 
        connectionString="Data Source=akhbarrr.mssql.somee.com;packet size=4096;User Id=YourUserID;Password=YourPassword;persist security info=False;Initial Catalog=akhbarrr" />
    <add name="akhbarrConnectionString" 
        connectionString="Data Source=akhbarrr.mssql.somee.com;packet size=4096;User Id=YourUserID;Password=YourPassword;persist security info=False;Initial Catalog=akhbarrr"
      providerName="System.Data.SqlClient" />
</connectionStrings>

參考/進一步閱讀:有關ASP.NET成員身份配置方面的更多信息,請參見: http : //msdn.microsoft.com/zh-cn/library/6e9y4s5t(v=vs.100).aspx

替代答案

由於您的會員資格申請名稱稱為MyApplication 你能確保ApplicationId在你的價值aspnet_Applications數據庫表中的值相匹配aspnet_Roles表為您的角色。

aspnet_Roles表中,您將找到當前ASP.NET成員身份角色的列表。 您是否可以確保您的角色在那里,並且ApplicationId值與aspnet_Applications表中的角色匹配。

您可以運行此查詢來查看已設置的角色以及針對哪個ASP.NET Membership應用程序的角色:

SELECT APP.ApplicationId, APP.ApplicationName, ROLES.RoleName, ROLES.RoleId 
FROM dbo.aspnet_Applications AS APP
JOIN dbo.aspnet_Roles AS ROLES ON APP.ApplicationId = ROLES.ApplicationId

這與用戶或角色無關。 該錯誤消息告訴您出什么問題和怎么做:您的程序無法訪問SQL數據庫,因此您需要檢查其設置是否正確。 具體來說,請檢查以下內容:

  • 您的SQL服務器正在運行
  • 您的SQL Server設置為接受來自程序的連接
  • 您的程序在Web.config文件中具有正確的連接字符串

您已經為成員資格提供程序定義了所有必要的詳細信息,但對於角色提供程序則沒有定義任何詳細信息!

<!-- you're providing all the details for the membership provider ...... -->
<membership defaultProvider="SqlProvider" userIsOnlineTimeWindow="15">
   <providers>
      <clear/>
      <add name="SqlProvider" type="System.Web.Security.SqlMembershipProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" connectionStringName="MembersConnectionString" applicationName="MyApplication" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" passwordFormat="Hashed"/>
      </providers>
</membership>

<!-- but nothing at all really for the role provider. -->
<roleManager enabled="true"/>

請參閱Scott Allen的博客文章,內容涉及如何定義角色提供者詳細信息,定義在何處查找信息以及使用哪個角色提供者-以及與角色提供者成員身份相同的操作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM