2015-07-20 75 views
1

請有人展示任何允許匿名訪問者的可能性,但不允許在c#mvc5中的操作方法上登錄用戶。如何允許匿名訪問者,但不允許在ASP.NET MVC 5中的操作方法登錄用戶?

在我的情況,我必須沒有隻允許某些特定的用戶按照自己的角色

EX用戶管理員角色不允許

但在Worker角色

允許用戶

+0

可能重複的[如何強制匿名訪問控制器操作?](http://stackoverflow.com/questions/29513828/how-to-force-only-anonymous-access-to-controller-action) – Kamo

+0

你的回答在我的情況下不正確 – Nirav

+0

什麼你想達到什麼目的?允許匿名用戶或特定角色的用戶? – greenhoorn

回答

0

最後,我得到正確的解決方案

public class AllowAnonymousOnlyAttribute : ActionFilterAttribute 
    { 
     public override void OnActionExecuting(ActionExecutingContext filterContext) 
     { 
      bool userId = HttpContext.Current.User.Identity.IsAuthenticated; 
      if (userId) 
      { 
       if (HttpContext.Current.User.IsInRole("Admin") || HttpContext.Current.User.IsInRole("Doctor")) 
       { 
        filterContext.HttpContext.Response.Redirect("/Home/Index"); 

       } 
      } 
      base.OnActionExecuting(filterContext); 
     } 
    } 
+0

感謝所有幫助我解決我的麻煩 – Nirav

相關問題