2015-07-03 148 views
1

我一直在開發一個站點一段時間,但最近有這個問題,主頁/索引頁面總是重定向到登錄頁面,除非你看不到你已經登錄,Home Controller被[AllowAnonymous] 包圍,我的defulat路線仍然是一樣的。C#-Asp.net MVC主頁/索引頁面重定向到登錄頁面

routes.MapRoute(
       name: "Default", 
       url: "{controller}/{action}/{id}", 
       defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
      ); 

我也不是什麼導致這個問題,我以前從來沒有過這樣的,我已經嘗試添加和刪除[使用AllowAnonymous],還是同樣的問題,但奇怪的是指數是唯一在家庭控制器中的操作需要登錄,否則工作正常

[AllowAnonymous] 
public class HomeController : Controller 
    { 
public ActionResult Index() 
     { 
      try 
      { 
       using (ApplicationDbContext db = new ApplicationDbContext()) 
       { 

        var types = (from r in db.FeedBack 
           select new FeedBackView { ID = r.ID, Name = r.Name, Jop = r.Jop, Body = r.Body }).Take(7).ToList(); 
        var SlidersList = (from r in db.Sliders 
             select new SliderView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList(); 
        var FeaturesList = (from r in db.Features 
             select new FeatureView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList(); 
        var AccordionList = (from r in db.Accordion 
             select new AccordionView { Description = r.Description, Title = r.Title, ImageURL = r.ImageURL }).ToList(); 
        var ServicesList = (from r in db.Services 
             select new ServiceView { Body = r.Body, Name = r.Name, ImageURL = r.ImageURL, Glaphicon = r.Glaphicon, ID = r.ID }).Take(6).ToList(); 

        var portfolioTypesList = (from r in db.PortfolioTypes 
               select new PortfolioTypeView { Name = r.Name, ID = r.ID, filter = r.filter }).Take(5).ToList(); 
        List<PortfolioView> Portfolios = new List<PortfolioView>(); 
        List<PortfolioView> toadd = new List<PortfolioView>(); 
        portfolioTypesList.ForEach(r => 
        { 
         //Portfolios.AddRange((from s in db.Portfolios 
         //      where s.PortfolioTypeID == r.ID 
         //      select new PortfolioView { TypeName = r.Name, ID = s.ID, filter = r.filter, ProjectName = s.ProjectName } 
         //      ).Take(6).ToList()); 

         toadd = (from s in db.Portfolios 
           where s.PortfolioTypeID == r.ID 
           select new PortfolioView { TypeName = r.Name, ID = s.ID, filter = r.filter, ProjectName = s.ProjectName }).Take(6).ToList(); 
         Portfolios.AddRange(toadd); 
        }); 

        var x = new List<PortfolioView>(); 
        PortfolioImage im = new PortfolioImage(); 
        Portfolios.ForEach(r => 
        { 
         im = db.PortfolioImages.Where(s => s.PortfolioID == r.ID).FirstOrDefault(); 
         if (im != null) 
         { 
          x.Add(new PortfolioView() 
          { 
           ID = r.ID, 
           TypeName = r.TypeName, 
           ProjectName = r.ProjectName, 
           URL = im.ImageURL, 
           filter = r.filter 
          }); 
         } 
         else 
         { 
          x.Add(new PortfolioView() 
          { 
           ID = r.ID, 
           TypeName = r.TypeName, 
           ProjectName = r.ProjectName, 
           URL = "", 
           filter = r.filter 
          }); 
         } 
        } 
          ); 

        var Articels = (from e in db.Articles 
            select new ArticleViewModel 
            { 
             Body = e.Body, 
             Title = e.Title, 
             ImageURL = e.ImageURL, 
             ID = e.ID 
            }).Take(3).ToList(); 

        //var Social = db.Others.FirstOrDefault(); 
        //ViewBag.Social = SocialMedia.convert(Social);     
        ViewBag.Articels = Articels; 
        ViewBag.portfolioTypesList = portfolioTypesList; 
        ViewBag.Portfolios = x; 
        ViewBag.ServicesList = ServicesList; 
        ViewBag.AccordionList = AccordionList; 
        ViewBag.SliderList = SlidersList; 
        ViewBag.FeaturesList = FeaturesList; 
        ViewBag.FeedBackGo = types; 
        return View(); 
       } 

      } 
      catch (Exception ex) 
      { 
       return RedirectToAction("Error"); 
      } 

     } 
} 
+1

控制器具有[授權]屬性?詳細說明你的問題... –

+0

不,控制器沒有[授權]屬性。 –

+0

我認爲向我們展示控制器代碼(或者至少是索引操作)會對您有所幫助 – Sebastiano

回答

0

您是否嘗試過自定義屬性?

例如,讓我們創建一個名爲CustomAuthorize

public class CustomAuthorize: AuthorizeAttribute  
{ 

    protected override void HandleUnauthorizedRequest(AuthorizationContext  
    filterContext)  
    {  
      if (IsUserAuthenticated(filterContext.HttpContext))  
      {  
      filterContext.Result = new RedirectResult("/Account/InvalidRole");  
      }  
      else  
      {  
      base.HandleUnauthorizedRequest(filterContext);  
      }  
    } 

    private bool IsUserAuthenticated(HttpContextBase context)  
    {  
      return context.User != null && context.User.Identity != null &&  
      context.User.Identity.IsAuthenticated;  
    } 

} 

默認情況下,授權屬性導致的登錄頁面,如果用戶不在正確的角色來訪問應用程序的某些部分的屬性。

現在要實現這一點,您應該執行以下操作。

[CustomAuthorize(Roles="Admin,Manager")] 
public class MyController 
{ 

     // Everyone has access 
     [AllowAnonymous] 
     public ActionResult Index() 
     { 
     return View(); 
     } 

     // Only Admin and Manager roles have access, everyone else is denied 
     public ActionResult About() 
     { 
     return View(); 
     } 

} 

請記住在每個人都可以訪問Action方法的情況下放置屬性[AllowAnonymous]。裏面的「公共」的方法可以包括以下內容,以允許特定角色的實現:

if(this.User.IsInRole("Administrator")) 
{ 

} 

有了,你將被重定向到/Account/InvalidRole,如果你不具備所需的角色。如果你很好的去頁面應該正常渲染。

+0

謝謝,但它沒有幫助 –

+0

請在類中和操作方法中添加一個斷點。它甚至跑班嗎?這只是與方法? – Aoren

+0

我在動作中添加了一個斷點,動作確實運行了,當它在重定向到登錄的佈局中到達@renderBody時,我按照步驟nad執行。 –