AliBazzi / IdentityServer4.Contrib.RedisStore

A persistence layer using Redis DB for operational data and for caching capability for Identity Server 4
https://www.nuget.org/packages/IdentityServer4.Contrib.RedisStore
MIT License
137 stars 48 forks source link

How to remove a key associated to user? #40

Closed luiarhs closed 4 years ago

luiarhs commented 4 years ago

Question Hi, I have one question...

Is there a way to remove(expire) a key associated with each user's in a kind of signout/logout event? I'm reading about IPersistedGrantStore.cs but it's not entirely clear to me.

Environment

AliBazzi commented 4 years ago

not sure I understand your question, can you reiterate ?

luiarhs commented 4 years ago

not sure I understand your question, can you reiterate ?

Sure, I want to remove(expire) a key associated with a specified user on a kind of logout/signout event. I explain? That's possible?

AliBazzi commented 4 years ago

you can do that by calling async Task RemoveAllAsync(PersistedGrantFilter filter) and pass in the filter properties the predicates you want to delete upon.

luiarhs commented 4 years ago

you can do that by calling async Task RemoveAllAsync(PersistedGrantFilter filter) and pass in the filter properties the predicates you want to delete upon.

Can you develop your answer please? Thanks in advance.

luiarhs commented 4 years ago

Hi @AliBazzi I must update the version of IdentityServer4.Contrib.RedisStore to version 4.0.0 to use the async Task RemoveAllAsync(PersistedGrantFilter filter) ?

If you had an example I'm would be very grateful to you.

Thanks.

AliBazzi commented 4 years ago

You don't have to use V4, if you are on V3, you can still use any of:

Task RemoveAsync(string key);
Task RemoveAllAsync(string subjectId, string clientId);
Task RemoveAllAsync(string subjectId, string clientId, string type);

I don't have example, it's up to you figure out how to consume the store.

luiarhs commented 4 years ago

after swimming in the IS deep pool that is how we found IS get hashed key:

private const string KeySeparator = ":";

/// <summary>
/// Gets the hashed key.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
protected virtual string GetHashedKey(string value)
{
    return (value + KeySeparator + GrantType).Sha256();
}

and this is how works for me

const string keySeparator = ":";
const string grantType = ""; //grant types: refresh_token, authorization_code

var key = HashExtensions.Sha256(refreshToken + keySeparator + grantType); // IdentityServer4.Models.HashExtensions

await _store.RemoveAsync(key); //PersistedGrantStore

Thank you!