The previous time with MVC + Redis did a simple single point web site. It's really a lot of dog problems.
Just can't sleep today, make a memo note >_<
The implementation method is very simple, just from overloading a controller or doing a actionfilterattribute can achieve the purpose.
The following is a controller code implementation,ActionFilterAttribute implementation similar :
Here I am convenient for the diagram Servicestack.redis although the latest free version has a lot of performance limitations (real pit dad).
Two controller classes, base for account information populated before action execution, one requirement must be logged in, or transferred to SSO
// <summary>/// Fill account user information for all action // </summary>
Public classBaseaccountcontroller:controller {protectedRedishelper.redishelper Redishelper {Get;Private Set; } protected ReadOnly StaticString Tokenkeycookie ="Qtmaccounttokencookie"; PublicBaseaccountcontroller () {Redishelper=NewRedishelper.redishelper (); } ~Baseaccountcontroller () {if(Redishelper! =NULL) {redishelper.dispose (); } } protected Override voidonactionexecuting (ActionExecutingContext filtercontext) {varCookie =Filtercontext.httpcontext.request.cookies[tokenkeycookie]; String token= filtercontext.httpcontext.request.querystring["Token"]; if(Cookie! =NULL) {token=cookies. Value; varAccountindex = redishelper.get<accountindex>(token); Viewbag.currentaccount=Accountindex; } Else if(!String.isnullorwhitespace (token)) { varAccountindex =Createtokencookie (token); Viewbag.currentaccount=Accountindex; } } /// <summary> ///Local cookie Creating token token/// </summary> /// <param name= "Token" >Token Tokens</param> /// <returns></returns> protectedAccountindex Createtokencookie (stringToken) { varAccountindex = redishelper.get<accountindex>(Token); //determine if a token token is valid if(Accountindex = =NULL) { return NULL; } //cookies that produce token tokens varToken_cookie =NewHttpCookie (Tokenkeycookie, Token) {HttpOnly=true, Secure=Formsauthentication.requiressl, Path="/", Expires= DateTime.Now.AddYears (Ten) }; HTTPCONTEXT.RESPONSE.COOKIES.ADD (Token_cookie); returnAccountindex; }
In
/// <summary> ///all action of the controller must hold a valid account token token. /// </summary> Public classAccountauthencontroller:baseaccountcontroller { PublicAccountauthencontroller ():Base() { } ~Accountauthencontroller () {if(Redishelper! =NULL) {redishelper.dispose (); } } protected Override voidonactionexecuting (ActionExecutingContext filtercontext) {String token= filtercontext.httpcontext.request.querystring["Token"]; varCookie =Filtercontext.httpcontext.request.cookies[tokenkeycookie]; if(Cookie = =NULL) { if(!String.isnullorwhitespace (token)) { //Create a new token local cookie when there is a new token token returned varAccountindex =Createtokencookie (token); if(Accountindex = =NULL) {filterContext.HttpContext.Response.Redirect (String.Format (actionuri.logi N, FilterContext.HttpContext.Request.Url.AbsoluteUri)); } } Else{filterContext.HttpContext.Response.Redirect (String.Format (Actionuri.login, filter Context.HttpContext.Request.Url.AbsoluteUri)); } } Else{token=cookies. Value; varAccountindex = redishelper.get<accountindex>(token); if(Accountindex = =NULL) { //Invalid token CookieFiltercontext.httpcontext.response.cookies[tokenkeycookie]. Expires = DateTime.Now.AddYears (-1); FilterContext.HttpContext.Response.Redirect (String.Format (Actionuri.login, FilterContext.HttpContext.Request.Url.AbsoluteUri)); } } Base. OnActionExecuting (Filtercontext); } #regionPrivate Method#endregion }
MVC SSO Landing Trouble ~