.net mvc結合微軟提供的FormsAuthenticationTicket登陸

一、Web.config

  <system.web><compilation debug="true" targetFramework="4.5" /><httpRuntime targetFramework="4.5" /><authentication mode="Forms"><forms loginUrl="/Sign/SignIn" defaultUrl="/Home/Index" /></authentication></system.web>
View Code

二、SignController(主要實現)

    /// <summary>/// 登陸、注銷功能/// </summary>public class SignController : Controller{/// <summary>/// 登陸頁面/// </summary>/// <returns></returns>
        [AllowAnonymous]public ActionResult SignIn(){var isAuthenticated = System.Web.HttpContext.Current.User.Identity.IsAuthenticated;if (isAuthenticated) //已經驗證用戶
            {return Redirect(FormsAuthentication.DefaultUrl);}var reUrl = FormsAuthentication.GetRedirectUrl(HttpContext.User.Identity.Name, false);ViewBag.RedirectUrl = reUrl;return View();}/// <summary>/// 登陸功能/// </summary>/// <param name="userName">用戶名</param>/// <param name="pwd">密碼</param>/// <returns></returns>
        [HttpPost][AllowAnonymous]public JsonResult LogIn(string userName, string pwd){if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(pwd)){var ticket = new FormsAuthenticationTicket(2, userName, DateTime.Now, DateTime.Now.AddMinutes(1), false, userName + pwd);string hashTicket = FormsAuthentication.Encrypt(ticket);var userCookie = new HttpCookie(FormsAuthentication.FormsCookieName, hashTicket);HttpContext.Response.Cookies.Add(userCookie);        //添加cookiesvar identity = new FormsIdentity(ticket);HttpContext.User = new CustomPrincipal(identity);       //獲取請求信息,通過自定義標志(重點)return Json(1);}elsereturn Json(0);}/// <summary>/// 注銷/// </summary>/// <returns></returns>
        [AllowAnonymous]public ActionResult SignOut(){FormsAuthentication.SignOut();return Redirect(FormsAuthentication.LoginUrl);}}
View Code

model

    /// <summary>/// 定義用戶對象的基本功能(自定義)/// </summary>public class CustomPrincipal : IPrincipal{#region 字段private IIdentity _identity;#endregion#region 屬性public IIdentity Identity{get { return _identity; }}#endregion#region 構造函數public CustomPrincipal(IIdentity identity){_identity = identity;}#endregion#region 方法public bool IsInRole(string role){throw new NotImplementedException();}#endregion}
View Code

view

@{ViewBag.Title = "SignIn";
}<h2>SignIn</h2>
<a href="@Url.Action("Index", "Home")">主頁</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陸</a>
<a href="@Url.Action("SignOut", "Sign")"> 注銷</a>
<a href="@Url.Action("Detail", "Home")"> 詳細</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>
<br />
<div>用戶名:<input id="user_name" /></div>
<div>密  碼:<input id="user_pwd" /></div>
<div><input id="btnSumbit" type="button" value="提交" /></div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script>$(function () {$('#btnSumbit').click(function () {var data = {userName: $('#user_name').val(),pwd: $('#user_pwd').val()}$.post('@Url.Action("LogIn", "Sign")', data, function (result) {if (result == 1)location.href = '@ViewBag.RedirectUrl';elsealert(result);});})})
</script>
View Code

三、需要權限的控制器(調用方式)

    [Authorize]public class BaseController : Controller{}
View Code
    public class HomeController : BaseController{/// <summary>/// 首頁/// </summary>/// <returns></returns>public ActionResult Index(){return View();}public ActionResult Detail(){var cidentity = (FormsIdentity)HttpContext.User.Identity;var data = cidentity.Ticket.UserData;//獲取存儲的 數據var name = HttpContext.User.Identity.Name;var dd = FormsAuthentication.FormsCookieName;return View();}}
View Code
@{ViewBag.Title = "Index";
}<h2>Index</h2>
<a href="@Url.Action("Index", "Home")">主頁</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陸</a>
<a href="@Url.Action("SignOut", "Sign")"> 注銷</a>
<a href="@Url.Action("Detail", "Home")"> 詳細</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>
View Code
@{ViewBag.Title = "Detail";
}<h2>Detail</h2>
<a href="@Url.Action("Index", "Home")">主頁</a>
<a href="@Url.Action("SignIn", "Sign")"> 登陸</a>
<a href="@Url.Action("SignOut", "Sign")"> 注銷</a>
<a href="@Url.Action("Detail", "Home")"> 詳細</a>
<h2>name:@HttpContext.Current.User.Identity.Name</h2>
View Code

?

轉載于:https://www.cnblogs.com/liujinwu-11/p/4551647.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/374964.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/374964.shtml
英文地址,請注明出處:http://en.pswp.cn/news/374964.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

vc6.o--fatal error C1010錯誤的解決

當編譯c文件時&#xff0c;出錯信息為&#xff1a;fatal error C1010: unexpected end of file while looking for precompiled header directive 解決方案&#xff1a; 1、如果發生錯誤的文件是由其他的C代碼文件添加進入當前工程而引起的&#xff0c;則AltF7進入當前工程的…

具有Java 7中自動資源管理功能的GC

這篇文章簡要概述了Java 7中引入的稱為自動資源管理或ARM的新功能。 文章探討了ARM如何減少開發人員為有效釋放分配的資源的JVM堆而必須編寫的代碼。 Java編程語言中編程的最甜蜜之處之一是對象取消分配的自動處理。 在Java世界中&#xff0c;這通常被稱為垃圾收集。 基本上&am…

PHP學習筆記(六)

《Wordpress 50個過濾鉤子》 1-10 過濾鉤子是一類函數&#xff0c;wordpress執行傳遞和處理數據的過程中&#xff0c;在針對這些數據做出某些動作之前的特定點執行。本質上&#xff0c;就是在wordpress輸出之前&#xff0c;將對瀏覽數據做出反應。 添加過濾鉤子&#xff1a; ad…

JS 操作 radio input(cc問卷管理)

1、選中特定的單選按鈕 function showDetail(content){$("input[name^radio]").removeAttr("checked");for(var i0;i<content.length;i){$("#radio"(i1)content.substr(i,1)).attr("checked","checked");} }2、手動添加問…

國內外著名黑客雜志

國外黑客雜志&#xff1a; 《phrack》黑客雜志 http://www.phrack.org 《phrack》創刊于80年代&#xff0c;是世界級的頂級黑客雜志&#xff0c;每年只有一期&#xff0c;現已出了65期&#xff0c;國人似乎至今只有三人在上面發表發表文章&#xff0c;三人好像都是綠盟的人&…

團體項目隨筆

我們的團體項目不僅在在課堂上討論了很久&#xff0c;課后也是幾經討論。每個人都有不同的想法我特別想做一個基于Web編寫的驢客網&#xff0c;因為基于個人需求&#xff0c;在最終的討論中被斃掉。 我們組最終的的討論結果是寫個游戲&#xff0c;關于游戲的發展&#xff0c;這…

Apache Lucene拼寫檢查器的“您是不是要”功能

Google的“您是不是要”功能 在上一篇文章中對Lucene進行了介紹之后 &#xff0c;現在是時候提高它&#xff0c;創建一個更復雜的應用程序了。 您肯定最熟悉Google的“您是不是要”功能&#xff08;其他搜索引擎也支持此功能&#xff09;。 這是一個例子&#xff1a; Lucene …

Android-做個性化的進度條

1.案例效果圖 2.準備素材 progress1.png(78*78) progress2.png(78*78) 3.原理 采用一張圖片作為ProgressBar的背景圖片(一般采用顏色比較淺的)。另一張是進度條的圖片(一般采用顏色比較深的圖片)。進度在滾動時&#xff1a;進度圖片逐步顯示&#xff0c;背景圖片逐…

匯編小記16/3/27

最后更新2016-03-27 21:05:06 [address]與[bx] [address] 在debug中mov ax,[0] 等價于mov ax,ds:[0] [0]表示內存偏移地址 但是在masm匯編解釋器中&#xff0c;mov ax,[0] 等價于mov ax,0 [0]表示常量0 [bx] mov ax,[bx] 表示 bx存放的數據為一個偏移地址&#xff0c;段…

ConcurrentLinkedHashMap v 1.0.1發布

大家好&#xff0c;我們發布了并發LinkedHashMap實現的1.0.1版本。 在最新版本中&#xff0c;已進行了一些較小的修改&#xff0c;以在多個線程遍歷映射的元素時提高性能。 最新版本還引入了可插拔驅逐策略。 當然&#xff0c;您可以實現自定義逐出策略&#xff0c;也可以將它…

BOMbing The System

roy g bivFebruary 2011 [Back to index] [Comments (0)] What is a BOM? Why should we care? Great, can we do that? Okay, lets do it! Unicode in files Greets to friendly people (A-Z) What is a BOM? Its not the thing that explodes. Thats a BOMB. Heh. BO…

鳥哥的linux私房菜學習筆記 ---第7章-2

1,文件內容查閱的命令: cat ,tac nl,more, less,head,tail ,od 文件的查閱參數,顯示行號如何顯示行號 nl 中的所有參數都是關于如何顯示行號的 這里面less的功能更多,更靈活 :空格 下一頁 pageup上一頁 pagedown 下一頁 /string 字符串查詢 ?string 反向字符串查詢 man的命…

HDU - 4497 GCD and LCM

題意&#xff1a;給出三個數的gcd,lcm&#xff0c;求這三個數的全部的可能 思路 &#xff1a;設x,y,z的gcd為d&#xff0c;那么設xd*a&#xff0c;yd*b&#xff0c;zd*c。a&#xff0c;b。c肯定是互質的。那么lcmd*a*b*c,所以我們能夠得到a*b*clcm/gcdans,將ans分解因數后&…

Java Lambda語法替代

關于lambda-dev郵件列表的討論已經開始解決lambdas /函數文字的Java語言語法應該是什么樣的問題。 讓我們看一個稍微平凡的例子&#xff0c;然后嘗試弄清楚問題。 Perl的人有一個很好的例子&#xff0c;說明以某種功能性的方式使用函數引用–他們稱其為Schwartzian變換&#xf…

淺析SMC技術

今天讓我們來看Win32ASM里面的高級一點的技術——SMC&#xff08;當當當當……&#xff09;&#xff01;&#xff01;&#xff01;SMC是什么意思&#xff1f;它的英文名叫“Self Modifying Code”&#xff0c;顧名思義&#xff0c;就是“代碼自修改”&#xff08;&#xff1f;&…

JAVA基礎--程序是順序執行的

class Testa {public static void main(String[] args) {String aa"aaa";String bb"bbb"aa;aa"cccc";System.out.println(bb);} } 輸出的是 “bbbaaa class Testa {public static void main(String[] args) {String aa"aaa";String …

Spring MVC攔截器示例

我以為是時候看看Spring的MVC攔截器機制了&#xff0c;這種機制已經存在了很多年&#xff0c;并且是一個非常有用的工具。 Spring Interceptor會按照提示進行操作&#xff1a;在傳入的HTTP請求到達您的Spring MVC控制器類之前對其進行攔截&#xff0c;或者相反&#xff0c;在其…

Android 調用系統的分享[完美實現同一時候分享圖片和文字]

android 系統的分享功能 private void share(String content, Uri uri){Intent shareIntent new Intent(Intent.ACTION_SEND); if(uri!null){//uri 是圖片的地址shareIntent.putExtra(Intent.EXTRA_STREAM, uri);shareIntent.setType("image/*"); //當用戶選擇短信時…

團隊行為守則—如果你們由我來領導

&#xfeff;&#xfeff;如果你是在我領導的團隊里&#xff0c;有幾個額外的事情我要告訴你。我深信這些行為守則是一個高效團隊的潤滑劑&#xff0c;我并不只是要求別人這樣做&#xff0c;我自己也嚴格恪守。 只有三樣事&#xff1a; 問&#xff1a;如果你對任務不清楚&#…

做短,但做對!

編寫簡潔&#xff0c;優雅&#xff0c;清晰的代碼一直是開發人員的艱巨任務。 您的同事不僅會感謝您&#xff0c;而且您會驚訝地發現&#xff0c;不斷期待著重構解決方案以更少的代碼完成更多&#xff08;或至少相同&#xff09;的工作是多么令人興奮。 曾經有人說好的程序員是…