InMemorySessionService

Namespace: ASPSecurityKit
Assembly: ASPSecurityKit.dll

Summary

Implements Dictionary<Tkey, TValue> based ISessionService to store and retrieve identity data for an authenticated session.

Constructors

#ctor()

Summary

Initializes a new instance of the InMemorySessionService class with empty dictionary storage and random sessionId (a guid).

Parameters

This constructor has no parameters.

#ctor(context,sessionId)

Summary

Initializes a new instance of the InMemorySessionService class.

Parameters
Name Type Description
context System.Collections.Generic.Dictionary<String, Object> The context (existing session data).
sessionId System.String The session identifier.

Properties

SessionID

Summary

Gets a unique identifier for the session.

Value

A unique identifier for the session.

Item

Summary

Gets or sets a session value by using the specified index.

Returns

The session-state value that is stored at the specified index. If index doesn’t exist, returns null.

Parameters
Name Type Description
index System.String The index of the session value.
Exceptions
Type Description
System.ArgumentNullException When index is null or whitespace.

Methods

Load(state,sessionId)

Summary

Re-initializes data in this instance of sessionService with the specified session-state replacing the existing one completely.

Parameters
Name Type Description
state System.Collections.Generic.Dictionary<String, Object> The new session-sate.
sessionId System.String The sessionId for the new session.
Exceptions
Type Description
System.ArgumentNullException When state is null or sessionId is null/whitespace.

Get<T>(key)

Summary

Gets a session value casted to type T by using the specified key.

Returns

A session value casted to type T by using the specified key. If value is not an instance of type T, it throws InvalidCastException.

Parameters
Name Type Description
key System.String The key to locate.
Generic Types
Name Description
T The type of the session value.
Exceptions
Type Description
System.ArgumentNullException When key is null or whitespace.
System.InvalidCastException When value is not an instance of T.
Remarks

If you aren’t sure whether the key exists or whether it can be casted to T, use TryGet<T> instead to avoid exception.

Put<T>(key,value)

Summary

Sets a session value by using the specified key.

Returns

Returns the same value just set.

Parameters
Name Type Description
key System.String The key to add to the session.
value T The value to be stored in the session against the key.
Generic Types
Name Description
T The type of value being set.
Exceptions
Type Description
System.ArgumentNullException When key is null or whitespace.
Remarks

If the key already exists, the existing value will be overridden.

TryGet<T>(key,value)

Summary

Safely Determines whether an element exists by the specified key in the session and if it does, returns its value as an out parameter. It returns false even if key is found but cannot be casted to T.

Returns

true if key is found and its value is successfully casted to T; otherwise, false.

Parameters
Name Type Description
key System.String The key to locate.
value T (Out) The out parameter that is set with the value upon success.
Generic Types
Name Description
T The type of the session value.
Exceptions
Type Description
System.ArgumentNullException When key is null or whitespace.

ContainsKey(key)

Summary

Determines whether this session instance contains an element by the specified key.

Returns

true if this session instance contains an element by the specified key; otherwise, false.

Parameters
Name Type Description
key System.String The key to locate in the session.
Exceptions
Type Description
System.ArgumentNullException When key is null or whitespace.

Remove(key)

Summary

Removes an element by the specified key from this session instance.

Returns

true if the key was found and removed; otherwise, false.

Parameters
Name Type Description
key System.String The key to remove from the session.
Exceptions
Type Description
System.ArgumentNullException When key is null or whitespace.

GetSessionState()

Summary

Gets the session data as dictionary.

Returns

The session data as dictionary.

Parameters

This method has no parameters.