Click or drag to resize

ContextFactoryCreateBySessionKey Method

Creates a new context of execution for the user behind the session key.

Namespace:  AskiaPortalCmn
Assembly:  AskiaPortalCmn (in AskiaPortalCmn.dll) Version: 1.7.0-build068
Syntax
public static IContext CreateBySessionKey(
	string sessionKey,
	string apiKey = null
)

Parameters

sessionKey
Type: SystemString
Session key (see BuildSessionKey)
apiKey (Optional)
Type: SystemString
Indicates the API key of the application that currently request a context. If null, we assume that the application in used is AskiaPortal.

Return Value

Type: IContext
Context of execution
Remarks

The context created will restrict the usage of API methods according to the role of the user.

If the session key is invalid, it return an invalid context.
You may check the validity of the context using Validate.

Examples

This example shows a typical usage of session key and context.

public void SignIn()
{
    string apiKey = Request.Params["apiKey"];
    string login = Request.Params["login"];
    string password = Request.Params["password"];
    var context = ContextFactory.CreateByLoginAndPassword(login, password, apiKey);
    var validation = context.Validate();
    if (!validation.Success)
    {
        throw validation.Exception;
    }

    var sessionKey = context.BuildSessionKey();
    if (!sessionKey.Success)
    {
        throw successKey.Exception;
    }

    var cookie = new HttpCookie("session", sessionKey.Value);
    Response.Cookies.Add(cookie);       
}

public void DoAuthenticatedAction()
{
    var apiKey = Request.Params["apiKey"];
    var sessionKey = Request.Cookies["session"].Value;
    var context = ContextFactory.CreateBySessionKey(sessionKey, apiKey);
    var validation = context.Validate();
    if (!validation.Success)
    {
        throw validation.Exception;
    }

    IUser currentUser = context.User;
    // Rest of the code...
}
See Also