解決 ASP.NET MVC 的 HtmlHelper 找不到的錯誤

這應該是剛開始學ASP.NET MVC 常犯的錯誤,一般我們會新增一個資料夾”Helpers”來放 HtmlHelper的程式,如在資料夾新增一個class,如:HMTLHelperExtensions.cs。

using System;
using System.Web.Mvc;

namespace DemoCustomIndentity.Helpers
{
    public static class HMTLHelperExtensions
    {
       public static string IsSelected(this HtmlHelper html, string controller = null, string action = null, string cssClass = null)
       {

          if (String.IsNullOrEmpty(cssClass))
          cssClass = "active";

          string currentAction = (string)html.ViewContext.RouteData.Values["action"];
          string currentController = (string)html.ViewContext.RouteData.Values["controller"];

          if (String.IsNullOrEmpty(controller))
          controller = currentController;

         if (String.IsNullOrEmpty(action))
         action = currentAction;

         return controller == currentController && action == currentAction ?
         cssClass : String.Empty;
       }

    public static string PageClass(this HtmlHelper html)
    {
       string currentAction = (string)html.ViewContext.RouteData.Values["action"];
       return currentAction;
     }
   }
}

閱讀全文〈解決 ASP.NET MVC 的 HtmlHelper 找不到的錯誤〉

使用專案範本實作 ASP.NET MVC Sitemap 及 Roles

Step01.新增一個含有identity的專案範本
Step02.將專案的identity角色功能打開

在App_Start的IdentityConfig.cs 加入

//增加角色管理員相關的設定
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
    public ApplicationRoleManager(IRoleStore<IdentityRole, string> roleStore): base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
    }
 }

在App_Start的 Startup.Auth.c s的 public void ConfigureAuth(IAppBuilder app) 加入

//增加角色的OwinContext
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

以上這兩個步驟,專案的identity 就具備Role的功能了

Step03.Install-Package MvcSiteMapProvider.MVC5 -Version 4.6.22

閱讀全文〈使用專案範本實作 ASP.NET MVC Sitemap 及 Roles〉

ASP.NET MVC identity 使用Role來判斷前後台使用者

在新起一個ASP.NET MVC專案時,我們可以使用含有identity的範本,來作為會員登入的系統,但是往往我們前台使用identity時,後台就會選擇使用Form Authentication,或者選擇空專案,全部使用Form Authentication。

這次的練習,我是採用前後台都是使用同一套identity,藉由Role的不同來切分前後台的使用者。
閱讀全文〈ASP.NET MVC identity 使用Role來判斷前後台使用者〉

解決phpMyAdmin連不上MySQL In App的問題


想要把Laravel PHP的程式部署到Azure上,原本覺得是一件很簡單的事情,因為前一陣子才在Azure上用WordPress建立我自己的部落格,前後也才花不到幾分鐘,一樣是用MySQL In App的方案(因為它便宜)。

台中的阿鴻來訊息說:phpMyAdmin連不上MySQL,我想說你這個肉腳,我來試試,再來教你,哈~哪來的自信阿….想不到竟然花了我約一週的時間,還好有Ming哥說他也被雷到,才找到一點蛛絲馬跡。

閱讀全文〈解決phpMyAdmin連不上MySQL In App的問題〉

使用ASP.NET MVC WebSecurity 過程記錄

需安裝兩個套件
Microsoft.AspNet.WebHelpers
WebMatrix.WebData

Web.config 的<system.web> 需增加

<system.web>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login" timeout="2880" />
</authentication>
<roleManager enabled="true" defaultProvider="SimpleRoleProvider">
<providers>
<clear />
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<clear />
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
</system.web>

如何將ASP.NET MVC全站鎖住

當使用ASP.NET MVC在開發管理後台或系統時,關於權限的設定,第一步就是先將全站鎖住,再將Login那一頁用[AllowAnonymous] 開放。

Step01.在App_Start的檔案夾中找得 FilterConfig.cs,增加
filters.Add(new AuthorizeAttribute());

public class FilterConfig
    {
        public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
            //增加這一行
            filters.Add(new AuthorizeAttribute());
        }
    }

Step02.需安裝兩個nuget套件
Microsoft.Owin.Host.SystemWeb
Microsoft.Owin.Security.Cookies

Step03.新增一個OWIN Startup的類別,並命名為Startup.cs

public class Startup
{
     public void Configuration(IAppBuilder app)
     {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            //作為辨識的的Cookie屬性
            AuthenticationType = "ApplicationCookie",
            //如果無權限存取401 最後導頁的位置
            LoginPath = new PathString("/Account/Login")
        });
    }
}