這兩天要做一個用戶登陸驗證的接口,用MVC這種action和view結(jié)合的做的話比較好做,但是直接使用webapi做的過程中遇到了不少困難,這里和大家分享一下==
由于兩者用的框架不一樣,當然webapi有自己的操作方法。
string user = HttpContext.Current.Request["user"]; //string pw = HttpContext.Current.Request["pw"]; HttpCookie cookie = System.Web.HttpContext.Current.Request.Cookies["user"]; //沒有cookie if (cookie == null) { //用戶正確 if (user.Equals("xcy")) { //設置客戶端cookie cookie = new HttpCookie("user"); cookie.Domain = Request.RequestUri.Host; cookie.Path = "/"; cookie.Values["user"] = user; //cookie.Values["user2"] = user; //cookie.Values.Add("user", user); //設置服務端session //HttpContext.Current.Response.Cookies[""] HttpContext.Current.Response.AppendCookie(cookie); HttpContext.Current.Session["user"] = user; return Ok("登陸成功"); } //用戶不正確 else { return Ok("用戶名不對"); } } //有cookie else { string session = HttpContext.Current.Session["user"].ToString(); string co = cookie.Values["user"]; if (co.Equals(session)) { return Ok("登陸成功"); } else { return Ok("登陸已過期"); } }代碼邏輯性就不要看了,直接看看操作方法就ok。二、用戶登錄驗證思路
第一種(基于session):
初次登陸時通過數(shù)據(jù)庫驗證,驗證成功則在session中保存用戶名“user”;
以后每次其他頁面使用的時候要進行session判斷看其中user是否有變量,有的話則默認進入頁面,沒有的話直接返回登錄界面;
第二種(基于cookie):
初次登陸的時候通過數(shù)據(jù)庫驗證,驗證成功則在cookie中設置user,讓瀏覽器每次帶著它返回(設置cookie生命周期還沒找到);
第二次登陸的時候獲取cookie看user是否有值,有的話則登陸成功,沒有的話則返回登錄界面;
第三種(第一種session的延伸)
原理基本和第一種一樣,但是不是用session保存,為了不同服務器數(shù)據(jù)的共享,保存在數(shù)據(jù)庫memcache中;
我采用的是第一種,比較簡單,而且性能要求也沒那么高,在MVC的WebApi中默認是沒有開啟Session會話支持的。需要在Global中重寫Init方法來指定會話需要支持的類型
public override void Init() { this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required); base.Init(); }三、webapi過濾器和mvc過濾器的區(qū)別
在各個頁面使用登錄驗證的時候都添加太麻煩,所以過濾器要派上用場。
這是mvc中的幾種過濾器
webapi中的過濾器類似使用方法并不完全相同。
當時我自己創(chuàng)建了一個過濾器如下
public class MyActionFilterAttribute: ActionFilterAttribute { public string name { get; set; } //public override void OnActionExecuting(ActionExecutingContext filterContext) //{ // base.OnActionExecuting(filterContext); // filterContext.HttpContext.Response.Write("開始時間:" + DateTime.Now.ToString() + name+"<br/>"); //} //public override void OnActionExecuted(ActionExecutedContext filterContext) //{ // base.OnActionExecuted(filterContext); // var controllerName = filterContext.RouteData.Values["controller"].ToString(); // var actionName = filterContext.RouteData.Values["action"].ToString(); // filterContext.HttpContext.Response.Write("結(jié)束時間:" + DateTime.Now.ToString() + "<br/>"); // filterContext.HttpContext.Response.Write("controller:" + controllerName + ",action:" + actionName); //} //public override void OnResultExecuting(ResultExecutingContext filterContext) //{ // base.OnResultExecuting(filterContext); // filterContext.HttpContext.Response.Write("開始時間:" + DateTime.Now.ToString() + "<br/>"); //} //public override void OnResultExecuted(ResultExecutedContext filterContext) //{ // base.OnResultExecuted(filterContext); // filterContext.HttpContext.Response.Write("開始時間:" + DateTime.Now.ToString() + "<br/>"); //} public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); Debug.WriteLine("ACTION 1 DEBUG PRe-processing logging"); //actionContext.Response. } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { base.OnActionExecuted(actionExecutedContext); Debug.WriteLine("ACTION 1 DEBUG OnActionExecuted Response " + actionExecutedContext.Response.StatusCode.ToString()); } } filter觸發(fā)不了 ,寫了一個filter的例子,繼承actionfilterattribute,死活觸發(fā)不了。搞了半天后來才搞明白,filter 繼承了mvc4的。原來webapi 在system.web.http命名空間下,mvc在System.web.mvc下,兩個空間都有filter,不知道怎么搞得,繼承mvc的了。上面注釋代碼是繼承mvc時使用的代碼==四、登錄界面保存的session獲取不到
這個把我都快搞炸了,后來分析后發(fā)現(xiàn)每次驗證的時候request中cookie值都沒有,分析了一下是不是跨域的問題,后來改調(diào)試模式為上線模式(我調(diào)試的時候前后端分開的),問題就好了===但是如何讓cookie在跨域的時候也包含到request里面還沒弄太清楚,貌似只有html和webservice在一個服務器上才可以==
初次涉及到用戶驗證,理解不是很深入,一知半解分享給大家,不明白的地方求大牛指點==
新聞熱點
疑難解答