瀏覽標籤:

C#

[C#][ASP.NET MVC5] 繼承 AuthorizeAttribute 來實作自訂驗證

有時候會需要頁面會需要依照使用者權限的不同,可以進入的頁面也不同,在MVC裡面有預設Role與User的方式來篩選使用者,不過有時候權限分細一點時就沒辦法應付了,這個時候就需要自訂驗證了。

 

權限表單的結構資料如下:

01

  1. 先在登入成功的地方放入一段程式,來把使用者有權限進入的頁面以字串的方式存入Session
    using (var db = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString))
    
    {
    	String account = HttpContext.User.Identity.Name.ToString();
    	var list = db.Query<int>(@"
    SELECT 
    [a].[List_Id]
    FROM [dbo].[Permissions] AS [a]
    WHERE [a].[User_Id] = 
    (	SELECT [Id]
    FROm [dbo].[AspNetUsers] AS [z]
    WHERE [z].Email = @Email)", new { Email = model.Email }).ToList<int>();
    
    	Session["Permissions"] = string.Join(",", list.ToArray());
    }

     

  2. 再來新建一個檔案名為CustomAuthorize.cs,程式碼如下:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace WebApplication_CustomVerification.Verification
    {
        public class CustomAuthorize : AuthorizeAttribute
        {
            public int ListId { get; set; }
    
            protected override bool AuthorizeCore(HttpContextBase httpContext)
            {
                if (httpContext == null)
                {
                    throw new ArgumentNullException("httpContext");
                }
    
                //判斷是否已驗證
                if (httpContext.User.Identity.IsAuthenticated == false)
                {
                    return false;
                }
    
                bool boolIsRight = false;
    
                //Session過期,要求重新登入
                HttpSessionStateBase session = httpContext.Session;
                if (session.Count != 0 &&
                    session["Permissions"] != null &&
                    session["Permissions"].ToString() != "")
                {
                    List<string> list = session["Permissions"].ToString().Split(',').ToList();
                    foreach (var item in list)
                    {
                        if (item == ListId.ToString())
                        {
                            boolIsRight = true;
                            break;
                        }
                    }
                }
    
                return boolIsRight;
            }
        }
    }

    這邊覆寫了原本驗證的機制,改成判斷先前存入Session內的字串。

  3. 這樣就可以在Action上面加上標籤來驗證使用者權限囉!
    using System.Web.Mvc;
    using WebApplication_CustomVerification.Verification;
    
    namespace WebApplication_CustomVerification.Controllers
    {
        public class HomeController : Controller
        {
    
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult About()
            {
                ViewBag.Message = "Your application description page.";
    
                return View();
            }
    
            public ActionResult Contact()
            {
                ViewBag.Message = "Your contact page.";
    
                return View();
            }
    
            [CustomAuthorize(ListId = 1111)]
            public ActionResult List_01()
            {
    
                return View();
            }
    
            [CustomAuthorize(ListId = 1112)]
            public ActionResult List_02()
            {
    
                return View();
            }
    
            [CustomAuthorize(ListId = 1113)]
            public ActionResult List_03()
            {
    
                return View();
            }
        }
    }

 

 

範例程式:https://github.com/shuangrain/WebApplication_CustomVerification

參考:[C#][ASP.NET MVC]自訂AuthorizeAttribute

       

[C#] 使用 HtmlAgilityPack 搜尋子節點輸出文字時出現亂碼

今天在撈取購物網站資料時發生了一件很詭異的事情,明明在HtmlNodeCollection搜尋時在父節點裡明明就是中文字,但在進行foreach篩選子節點時卻發生原本的中文decode了  !!!!
經過了三四個小時的google與詢問大大,終於有了結果。

  1. 在父節點裡是正常顯示中文01
  2. 到了子節點卻發生decode的現象02
  3. 將字串貼入記事本用IE開啟即正常顯示中文
    &#39208;&#38263;&#25512;&#34214;

    03

解法如下:

  • 使用HttpUtility.HtmlDecode這個方法來恢復正常文字
    HttpUtility.HtmlDecode(nodes_proName[i].InnerText)

    04

       

[C#] 使用 HtmlAgilityPack 來採集網頁

因為工作需求,所以必須寫一隻小程式來擷取網頁資料,上網Google了一下於是找到了一個好用的套件HtmlAgilityPack,可以迅速的過濾HTML標籤,取得網頁資料。

 

  1. 從NuGet安裝HtmlAgilityPack 

    01

  2. 讀取網頁(以原價屋為例)
    //指定來源網頁
    WebClient url = new WebClient();
    //將網頁來源資料暫存到記憶體內
    MemoryStream ms = new MemoryStream(url.DownloadData("http://www.coolpc.com.tw/evaluate.php"));
    //使用預設編碼讀入 HTML 
    HtmlDocument doc = new HtmlDocument();
    doc.Load(ms, Encoding.Default);
  3. 原價屋除了商品的類型以外,類型內的選單(select)有做群組分類,為了要將分類與商品所以先建一個Model
    class Product
    {
    	public string type { get; set; }
    
    	public string name { get; set; }
    }
  4. 判斷所要的商品類型(以CPU為例),再擷取該商品類型內選單的分類
    var list_type = new List<string>();
    HtmlNodeCollection nodes = doc.DocumentNode.SelectNodes("//select[@name='n4']//optgroup");
    
    foreach (HtmlNode node in nodes)
    {
    	var type = node.Attributes["label"].Value;
    	list_type.Add(type);
    }
  5. 接下來就要該使擷取商品名稱與價格了
    List<string> list_name = doc.DocumentNode.SelectSingleNode("//select[@name='n4']").InnerText.Split('\n').ToList();
    
    //刪除不必要的非商品選項
    list_name.RemoveRange(0,3);
    list_name.RemoveAt(list_name.Count - 1);
  6. 將商品類型與名稱填入Model
    var models = new List<Product>();
    int number = 0;
    for (int i = 0; i < list_name.Count; i++)
    {
    	string type = list_type[number];
    	string name = list_name[i];
    
    	if (name == "")
    	{
    		number++;
    	}
    	else
    	{
    		models.Add(new Product()
    		{
    			type = type,
    			name = name
    		});
    
    		Console.WriteLine("類型:{0} ,", type);
    		Console.WriteLine("名稱:{0}", name);
    	}
    }
    
    Console.ReadLine();
  7. 執行結果0000

 

範例程式:https://github.com/shuangrain/ConsoleApplication_HtmlAgilityPack

 

參考:[ASP.NET][C#]使用HtmlAgilityPack(1) 擷取網頁上的股票

       

[C#][ASP.NET MVC5] 使用 jQuery Form Plugin 與 HttpPostedFileBase 檔案上傳

先前提到過 [C#][ASP.NET MVC5] 使用 HttpPostedFileBase 檔案上傳 ,這次我要使用Ajax的方式上傳檔案,但研究了Ajax.BeginForm許久都無法上傳檔案,最後找到使用jQuery Form Plugin來實作此功能。

來源:

  1. ASP.NET MVC – 使用 jQuery Form Plugin 做檔案上傳
  2. ASP.NET MVC – 使用 jQuery Form Plugin 做檔案上傳之加點東西

 

使用NuGet安裝所需套件

  1. 安裝jQuery Form Plugin
    01
  2. 安裝Javascript Alert:toastr
    02

 

View

@{
    ViewBag.Title = "Upload";
}
<h2>Upload</h2>
<hr />
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    <div class="form-horizontal">
        <div class="form-group">
            <label class="control-label col-sm-2">選擇檔案</label>
            <div class="col-sm-10">
                <input type="file" name="file">
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="提交" class="btn btn-primary" />
            </div>
        </div>
    </div>
}
@section css
{
    @Styles.Render("~/Content/toastr")
}
@section scripts
{
    @Scripts.Render("~/bundles/jqueryajax")
    @Scripts.Render("~/bundles/jqueryform")
    @Scripts.Render("~/bundles/toastr")
    <script>
        $("form").ajaxForm({
            iframe: true,
            dataType: "json",
            success: function (result) {
                $("form").resetForm();
                if (result.success) {
                    toastr.success(result.message, 'Success Message')
                }
                else {
                    toastr.error(result.message, 'Error Message')
                }
            },
            error: function (xhr, textStatus, errorThrown) {
                $("form").resetForm();
                toastr.error('檔案上傳錯誤.', 'Error Message')
            }
        });
    </script>
}

HTML的部分可以不用更動,無須使用Ajax.BeginForm,只需要在JavaScript的地方使用ajaxForm即可將表單轉換成Ajax模式。

Controller

[HttpPost]
public JsonResult Upload(HttpPostedFileBase file)
{
	if (file != null && file.ContentLength > 0)
	{
		var fileName = Path.GetFileName(file.FileName);
		var path = Server.MapPath("~/App_Data/FileUploads");
		//若該資料夾不存在,則新增一個
		if (!Directory.Exists(path))
		{
			Directory.CreateDirectory(path);
		}
		path = Path.Combine(path, fileName);
		file.SaveAs(path);
		return Json(new
		{
			success = true,
			message = fileName,
			ContentLenght = file.ContentLength
		}, "text/html");
	}
	else {
		return Json(new
		{
			success = false,
			message = "請上傳正確的檔案."
		}, "text/html");
	}
}

由於前端使用Ajax所以後台回傳資訊則必須從ActionResult修改成JsonResult,這樣才能強制回傳Json格式,需要注意的一點是在IE底下若無指定contentType的話則會出現問題如下圖,必須指定回傳的contentTypetext/html才能在IE底下正常接收資訊。

03

       

[C#][ASP.NET MVC5] MySQL Entity Framework 學習筆記(一) – 環境設定

剛開始在碰ASP.NET MVC的時候,查詢該如何連結資料庫時常常會聽到Entity Framework這個方法,但由於那時沒有太多時間,沒去查太多資料都用SQL自幹,現在閒下來了,就來玩玩看這個東西,首先是環境設定:

  1. NuGet安裝MySQL使用Entity Framework時所需要的套件
    01
     
  2. 總共有三個東西要安裝,分別是
    • MySql.Data
    • MySql.Data.Entity
    • MySql.Web

    01

  3. 安裝完畢後就要開始設定連結字串,打開Web.config新增連結字串
    <connectionStrings>
    	<add name="MySqlConnectionString" providerName="MySql.Data.MySqlClient" connectionString="Server=server;Database=dbname;UID=username;Password=password" />
    </connectionStrings>

    02

  4. 建立MySqlContext.cs並繼承DbContext供後續使用
    public class MySqlContext : DbContext
    {
    	public MySqlContext() : base("MySqlConnectionString") { }
    }

 

結論:

這樣環境就建置完成囉,後續就可以開始使用Entity Framework了!

 

參考:

  1. ASP.NET MVC, ENTITY FRAMEWORK CODE FIRST與MYSQL筆記
  2. Entity Framework 快速上手與學習資源整理
       

[C#][ASP.NET MVC5] FileUpload 上傳檔案大小的限制

ASP.NET為防止駭客利用大檔案進行DOS(Denial Of Service)攻擊,所以把上傳檔案大小限制在4096KB(4MB),因此當上傳檔案超過4MB時,就會收到System.Web.HttpException 超出最大的要求長度的錯誤訊息如下:

01

那如果需要上傳超過4MB的檔案怎麼辦?那就必須修改Web.configsystem.web讓該站自訂大小,修改內容如下:

<system.web>
	<httpRuntime targetFramework="4.5" maxRequestLength="102400" executionTimeout="600"/>
</system.web>
  • maxRequestLength 檔案大小(KB)
  • executionTimeout 上傳時間(秒)

修改完後雖然不會跳出上面的錯誤了,不過卻跳出另外一個訊息:
04

拜了Google才發現,原來 Windows Server 2008 (IIS 7.0) 上又多了一個 maxAllowedContentLength 屬性 (單位為 Byte),於是乎又打開了Web.config找到system.webServer,修改內容如下:

<system.webServer>
	<security>
		<requestFiltering>
			<requestLimits maxAllowedContentLength="1073741824" />
		</requestFiltering>
	</security>
</system.webServer>

其預設值為 30000000 Bytes (~28.6 MB),所以上傳 30 MB 的檔案就會產生錯誤了。這邊我是修改為1 GB (1024 x 1024 x 1024)可視情況調整。


綜合以上的修改結果為:

02

 

如果使用者上傳超過設定的最大上限怎麼辦?是不是又會跳出錯誤訊息?不要害怕,只要在Global.asax裡面處理這項錯誤即可,處理方式如下:

protected void Application_BeginRequest(object sender, EventArgs e)
{
	HttpRuntimeSection section = (HttpRuntimeSection)ConfigurationManager.GetSection("system.web/httpRuntime");
	int maxFileSize = section.MaxRequestLength * 1024;
	if (Request.ContentLength > maxFileSize)
	{
		try
		{
			Response.Redirect("~/SizeError.aspx");
		}
		catch (Exception ex)
		{
			Logger logger = LogManager.GetCurrentClassLogger();
			logger.Warn(ex.Message);
		}
	}
}

03

 

結論:

一個簡單的上傳功能,想不到有這麼多眉眉角角需要注意,做這行真是不容易QQ

 

參考:

  1. ASP.NET如何設定檔案上傳大小可超過預設4096KB(4MB)
  2. FileUpload 上傳檔案大小的限制
       

[C#][ASP.NET MVC5] 使用 HttpPostedFileBase 檔案上傳

檔案上傳功能在網頁中常常出現,在ASP.NET MVC裡面要上傳檔案非常簡單,這裡簡單筆記一下有關 ASP.NET MVC 的檔案上傳基本操作方法。

  • Controller
    [HttpPost]
    public ActionResult Upload(HttpPostedFileBase file)
    {
    	if (file != null && file.ContentLength > 0)
    	{
    		var fileName = Path.GetFileName(file.FileName);
    		var path = Server.MapPath("~/App_Data/FileUploads");
    		//若該資料夾不存在,則新增一個
    		if (!Directory.Exists(path))
    		{
    			Directory.CreateDirectory(path);
    		}
    		path = Path.Combine(path, fileName);
    		file.SaveAs(path);
    	}
    	return RedirectToAction("Upload");
    }
  • View
    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        <div class="form-horizontal">
            <div class="form-group">
                <label class="control-label col-sm-2">選擇檔案</label>
                <div class="col-sm-10">
                    <input type="file" name="file">
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input type="submit" value="提交" class="btn btn-primary" />
                </div>
            </div>
        </div>
    }

    這邊要注意的是,如需要使用HttpPostedFileBase上傳檔案的話,則必須添加enctype = "multipart/form-data",不然怎麼樣都沒辦法正常收到資料!
    另外如果使用下列語法是無法接收資料的,因這樣表單並沒有含enctype = "multipart/form-data"這項

    @using (Html.BeginForm("Upload", "Home", new { enctype = "multipart/form-data" }))

    一定要用改為下面的方法

    @using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

 

範例程式:https://github.com/shuangrain/WebApplication_Upload

       

[C#] 檢查資料夾是否存在,如不存在則新增資料夾

在儲存檔案的時候,常常會需要動態新增資料夾,但如果沒有特別處理的話,資料夾無法正常建立,所以需要用到下面的語法來檢查該資料夾是否存在,如不存在則新增。

using System.IO;
if (Directory.Exists(資料夾路徑))
{
    //資料夾存在
}
else
{
    //新增資料夾
    Directory.CreateDirectory(@"C:\temp\");
}

P.S. 寫入檔案除了要注意資料夾路徑是否存在以外,還要注意該應用程式的權限問題唷!

       

[C#][ASP.NET MVC5] 使用Ajax.BeginForm時無法正常運作

在ASP.NET MVC裡面,表單提交方法有兩種,一種是傳統的Html.BeginForm另外一種是利用JQuery實現的Ajax.BeginForm,平常我都是使用Html.BeginForm在處理表單,最近在寫上傳檔案功能的時候想玩Ajax.BeginForm看看,於是乎發生了Ajax無法正常運作的問題,拜了Google大神很久發現了原因是Visual Studio新建專案的時候沒有幫我加入Microsoft jQuery Unobtrusive Ajax這個套件,多虧這個原因讓我撞牆撞了好久,最後裝上去才解決問題。

解決步驟如下:

  1. 對著專案點選右鍵選擇管理NuGet套件
    01
  2. 搜尋unobtrusive找到Microsoft.jQuery.Unobtrusive.Ajax選擇安裝
    02
  3. /App_Start/BundleConfig.cs裡面新增套件
    03

    bundles.Add(new ScriptBundle("~/bundles/jqueryajax").Include(
                "~/Scripts/jquery.unobtrusive-ajax*"));
  4. 在要使用的頁面加入以下語法使用
    @Scripts.Render("~/bundles/jqueryajax")
       

[C#] 使用 Google Maps Geocoding API 將地址轉換為經緯度

本來是使用Google Map JavaScript API的google.maps.Geocoder,但發現Google似乎有對查詢間隔做限制,Code常常一下可以一下又不行,找了很久終於找到了不錯的C#解決方案,而且似乎沒有任何限制唷!

var address = "台中市沙鹿區台灣大道六段1018號";
var requestUri = string.Format("http://maps.googleapis.com/maps/api/geocode/xml?address={0}", Uri.EscapeDataString(address));

var request = WebRequest.Create(requestUri);
var response = request.GetResponse();
var xdoc = XDocument.Load(response.GetResponseStream());

var result = xdoc.Element("GeocodeResponse").Element("result");
var locationElement = result.Element("geometry").Element("location");
var lat = locationElement.Element("lat");
var lng = locationElement.Element("lng");

如果回傳格式想要是Json的話把requestUri的xml改成json就好囉!

來源:How to call Google Geocoding service from C# code