1

の1- AuthorizeUserAttribute.csを新しいレコードを作成禁止はMVCのHTTPエラー403.14 - 後

public class AuthorizeUserAttribute : AuthorizeAttribute 

{ 

public string AccessLevel { get; set; } 
protected override bool AuthorizeCore(HttpContextBase httpContext) 
{ 
    var isAuthorized = base.AuthorizeCore(httpContext); 
    if (!isAuthorized) 
     return false; 

    if (this.AccessLevel.Contains("Admin")) 
    { 
     return true; 
    } 
    else return false; 
} 

2-これは私のコントローラ

[AuthorizeUser(AccessLevel = "Admin")] 

public class ProductsController : Controller 

{ 
private DataBaseContext db = new DataBaseContext(); 
public ActionResult Index() 
{ 
    var product = db.Product.Include(p => p.ProductGroup); 
    return View(product.ToList()); 
} 

} 



[AuthorizeUser(AccessLevel = "Admin")] 

public ActionResult Create([Bind(Include = "Product_Id,ProductName,Description,PicUrl,Group_Id")] Product product) 
{ 
    if (ModelState.IsValid) 
    { 
     db.Product.Add(product); 
     db.SaveChanges(); 
     return RedirectToAction("Index"); 
    } 

    ViewBag.Group_Id = new SelectList(db.ProductGroups, "Group_Id", "GreoupName", product.Group_Id); 
    return View(product); 
} 

3-FilterConfig.csである衣装AUTHORIZE属性のクラスですSTART_UPフォルダ

public class FilterConfig 

{ 
public static void RegisterGlobalFilters(GlobalFilterCollection filters) 
{ 
    filters.Add(new HandleErrorAttribute()); 
    filters.Add(new AuthorizeAttribute()); 
    filters.Add(new AuthorizeUserAttribute()); 

} 
} 

4-Global.asax.cs

禁断のページ -

[HttpPost] 
public ActionResult Login(LoginViewModel model) 
{ 
    if (!ModelState.IsValid) //Checks if input fields have the correct format 
    { 
     return View(model); //Returns the view with the input values so that the user doesn't have to retype again 
    } 

      if(model.Email == "[email protected]" & model.Password == "@1234psm") 
       { 
      var identity = new ClaimsIdentity(new[] { 
              new Claim(ClaimTypes.Name,"Admin"), 
              new Claim(ClaimTypes.Email, "[email protected]"), 
              new Claim(ClaimTypes.Role,"Admin") 

              }, "ApplicationCookie"); 

      var ctx = Request.GetOwinContext(); 
      var authManager = ctx.Authentication; 
      authManager.SignIn(identity); 

        return Redirect(GetRedirectUrl(model.ReturnUrl)); 
       } 
    ModelState.AddModelError("", "incorrect UserName or pass"); 
    return View(model); 


} 

は後に新製品を作成し、製品/ショーHTTPエラー403.14に戻ってログインし、などのため

void Application_Start(object sender, EventArgs e) 
{ 
    // Code that runs on application startup 
    AreaRegistration.RegisterAllAreas(); 
    GlobalConfiguration.Configure(WebApiConfig.Register); 
    RouteConfig.RegisterRoutes(RouteTable.Routes);  
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); 
    AntiForgeryConfig.UniqueClaimTypeIdentifier = ClaimTypes.NameIdentifier; 

} 

5 Admin1Controller.cs ...。書き込みプロダクト/インデックスの正しいページを表示

+0

@SimonWhiteheadあなたがこの問題を知っているなら助けてください –

答えて

0

まず、カスタム属性にAccessLevelプロパティを設定するコードはありません。たぶんあなたはそれを投稿していないかもしれませんが、これがあなたのコードであれば、なぜこれがうまくいかないのかは明らかです。AccessLevelは常にnullなので、文字列 "Admin"は決して含まれません。

つまり、カスタム属性は必要ありません。 AuthorizeAttributeは既にロールを処理しています。それは、あなたが何らかの並行ロールのような機能を実装しようとしているようですが、それは時間の無駄です。ちょうど:

[Authorize(Roles = "Admin")] 

そしてそれを1日と呼びます。

+0

私はAccessLevelが必要でしょうか教えてください。AccessLevelがnullである理由を教えてください。 –

+0

これは決して値を割り当てていない自動実装のプロパティです。 –

+0

あなたはそれを修正する方法を知っていますか –

関連する問題