MVC SSO Landing Trouble ~

Source: Internet
Author: User
Tags httpcontext

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 ~

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.