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

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 });
}
}
I am using JWT's with this: https://github.com/openiddict/openiddict-core
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.
@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 https://github.com/MarkPieszak/aspnetcore-angular2-universal