前言: 最新開發專案要求做授權,專案是MVC+Webapi,這里只說MVC里的授權,初步學習授權內容,簡單總結一下:
方法里設定權限示例:
[Authorize] //普通授權 [Authorize(Roles = "admin")] //角色授權 [Authorize(Users = "admin")] //用戶授權
【Main】核心代碼1:
//設定授權方式一(授權模式:通用授權:[Authorize]+用戶授權:[Authorize(Users = "admin")]): //過期時間在配置節點的timeout //FormsAuthentication.SetAuthCookie(loginUser.Name, true); //設定授權方式二(存盤用戶資料=角色等,ticket有效期30天)(授權模式為:通用授權+用戶授權+角色授權:[Authorize(Roles = "admin")]); //這里僅是角色資訊存盤,具體的角色設定是在Global.aspx.cs的方法Application_AuthenticateRequest中設定new GenericPrincipal(identity, new string[] { role })起的作用; FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, name, DateTime.Now, DateTime.Now.AddDays(30), true, loginUser.Role); HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket)); // 設定cookie的過期時間(這里是1小時) cookie.Expires = DateTime.Now.AddHours(1); Response.Cookies.Add(cookie);
【Main】核心代碼2:
protected void Application_AuthenticateRequest(object sender, EventArgs e) { // 取得認證Check的Cookie HttpCookie cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName]; if (cookie == null) return; // 解密 FormsAuthenticationTicket ticket = null; try { ticket = FormsAuthentication.Decrypt(cookie.Value); } catch (Exception) { return; } if (ticket == null) return; // 取得ticket.UserData中設定的角色 string role = ticket.UserData; // From認證中,使用IPrincipal物件中的GenericPrincipal類, // 該類由表示資格情報的FormsIdentity類和角色資訊(string[]物件)組成, FormsIdentity identity = new FormsIdentity(ticket); GenericPrincipal principal = new GenericPrincipal(identity, new string[] { role }); // 把FormsIdentity賦值到Context.User中 // 可以從Page.User中取得該值 Context.User = principal; }
以下是原文:轉載自:【MVC5】使用權限+角色
1.在Ticket中設定用戶角色
在權限的Ticket中設定用戶的角色(這里是逗號分割),
List<string> roles = new List<string>();
if (isAdmin) {
roles.Add("Admin");
}
roles.Add("Guest");
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
1,
model.UserId,
DateTime.Now,
DateTime.Now.AddDays(30), // 設定記住登錄的時間(這里是30天)
true,
String.Join(",", roles)); // 設定用戶角色
HttpCookie cookie = new HttpCookie(FormsAuthentication.FormsCookieName, FormsAuthentication.Encrypt(ticket));
// 設定cookie的過期時間(這里是50年)
cookie.Expires = DateTime.Now.AddYears(50);
Response.Cookies.Add(cookie);
2.在Global.asax.cs中添加Application_AuthenticateRequest方法
protected void Application_AuthenticateRequest(object sender, EventArgs e) {
// 取得認證Check的Cookie
HttpCookie cookie = Context.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie == null) return;
// 解密
FormsAuthenticationTicket ticket = null;
try {
ticket = FormsAuthentication.Decrypt(cookie.Value);
}
catch (Exception) {
return;
}
if (ticket == null) return;
// 取得ticket.UserData中設定的角色
string[] roles = ticket.UserData.Split(new char[] { ',' });
// From認證中,使用IPrincipal物件中的GenericPrincipal類,
// 該類由表示資格情報的FormsIdentity類和角色資訊(string[]物件)組成,
FormsIdentity identity = new FormsIdentity(ticket);
GenericPrincipal principal = new GenericPrincipal(identity, roles);
// 把FormsIdentity賦值到Context.User中
// 可以從Page.User中取得該值
Context.User = principal;
}
3.在Controller中使用Authorize特性
// 允許匿名訪問
[AllowAnonymous]
public class HomeController : Controller
{
......
}
// 只允許登錄用戶訪問
[Authorize]
public class SampleController : Controller
{
......
}
// 只允許具有"Admin"角色的用戶訪問
[Authorize(Roles = "Admin")]
public class SampleController : Controller
{
......
}
// 該特性同樣可用于Action
參照:http://www.atmarkit.co.jp/ait/articles/0307/26/news002_2.html
已同步到【MVC5】ASP.NET MVC 專案筆記匯總
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/206032.html
標籤:.NET技术
上一篇:WPF 關于繪圖個人總結
下一篇:WPF 關于繪圖個人總結
