JavaScriptServices icon indicating copy to clipboard operation
JavaScriptServices copied to clipboard

【discuss】keep the same token for both client/server side

Open seven1986 opened this issue 9 years ago • 5 comments

21

seven1986 avatar Mar 25 '17 15:03 seven1986

Server Cookies auth code, keep the same token in client/server side nuget: Microsoft.AspNetCore.Authentication.Cookies

1,Add a Property in basic class for every controller

 protected string AccessToken
        {
            get
            {
                // 已授权
                if (User.Identity.IsAuthenticated)
                {
                   return User.FindFirst(ClaimTypes.Name).Value;
                }
                else
                {
                    // 未授权
                   // RequestToken that is how you get token from your api server
                    var token = RequestToken().Result;
                    
                  // call signin function make cookies authen
                    SignIn(token);

                    return token;
                }
            }
        }

2,SignIn function,基于cookies做授权 是为了保持token在client和server一致

        protected async void SignIn(string token)
        {
            var AuthenClaim = User.FindFirst(ClaimTypes.Name);

            // 防重复,如果已经添加过授权,就更新token,否则新增
            if (AuthenClaim != null)
            {
                if (!token.Equals(AuthenClaim.Value))
                {
                    var Identity = User.Identity as ClaimsIdentity;

                    if (Identity.TryRemoveClaim(AuthenClaim))
                    {
                        Identity.AddClaim(new Claim(ClaimTypes.Name, token));

                        var principal = new ClaimsPrincipal(Identity);

                        await HttpContext.Authentication.SignInAsync(Constant.AuthenticationScheme.Cookies, principal);
                    }
                }
            }

            else
            {
                var identity = new ClaimsIdentity(Constant.AuthenticationScheme.Cookies);

                identity.AddClaim(new Claim(ClaimTypes.Name, token));

                var principal = new ClaimsPrincipal(identity);

                await HttpContext.Authentication.SignInAsync(Constant.AuthenticationScheme.Cookies, principal,
                    new AuthenticationProperties { IsPersistent = true });
            }
        }

seven1986 avatar Mar 28 '17 06:03 seven1986

I am using JWT's with this: https://github.com/openiddict/openiddict-core

paddyza avatar Mar 28 '17 06:03 paddyza

I'm glad you've got a solution that works for you with this.

Longer term, we're aiming to have a more built-in solution. That's being tracked in #548.

SteveSandersonMS avatar Mar 29 '17 10:03 SteveSandersonMS

@seven1986 hi bro can i ask whats that requestToken.result i cant seem to find it. i also need the cookie server side thanks

jeinz11 avatar May 29 '17 11:05 jeinz11

@jeinz11 https://github.com/MarkPieszak/aspnetcore-angular2-universal

seven1986 avatar May 31 '17 03:05 seven1986