簡體   English   中英

ASP.NET MVC自定義角色提供程序不起作用

[英]ASP.NET MVC custom role provider not working

我已經設置了一個使用以下代碼實現的自定義角色提供程序,只是似乎從未使用過它,而是使用了默認提供程序。 當使用[Authorize(Roles = "Administrator")]屬性裝飾HomeController時,將調用CustomRoleProvider構造函數(我只包括該構造函數以查看是否會遇到斷點),但是沒有一個方法被調用。 然后,我看到HTTP Error 401.0 - Unauthorized頁面。

除了將必要的位添加到web.config之外,我還沒有執行其他任何操作來使Windows身份驗證正常工作。 我認為這是可行的,因為如果我不包含<allow users="*"></allow> (顯然不包含Authorize屬性), 401.2.: Unauthorized: Logon failed due to server configuration得到401.2.: Unauthorized: Logon failed due to server configuration錯誤, 401.2.: Unauthorized: Logon failed due to server configuration ,所以我假設我正在被認證。

我已經按照這篇SO帖子清除了我的瀏覽器cookie,但這沒有任何效果。

CustomerRoleProvider

public class CustomRoleProvider : RoleProvider
{
    public CustomRoleProvider()
    {            
    }

    public override bool IsUserInRole(string username, string roleName)
    {
        bool isUserInRole = false;

        // omitted for brevity

        return isUserInRole;
    }

    public override string[] GetRolesForUser(string username)
    {
        string[] roles = null;

        // omitted for brevity

        return roles;
    }

    // omitted for brevity

}

web.config

    <authentication mode="Windows">
    </authentication>
    <authorization>
      <allow users="*"></allow>
      <deny users="?"/>
    </authorization>
    <roleManager defaultProvider="CustomRoleProvider" enabled="true">
      <providers>
        <clear />
        <add name="CustomRoleProvider" type="ProjectName.UI.Mvc.Code.Providers.CustomRoleProvider" />
      </providers>
    </roleManager>

家庭控制器

[Authorize(Roles = "Administrator")]
public class HomeController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

這是由於未將IIS Express設置為使用Windows身份驗證引起的。 要修復,我在解決方案資源管理器中選擇了項目,打開了屬性窗口,設置Windows Authentication = EnabledAnonymous Authentication = Disabled 自定義角色提供程序現在可以工作。

暫無
暫無

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

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