Skip to main content

ApiKeyAuthProvider

Enable access to protected Services using API Keys

Assembly: ServiceStack.dll
View Source
Declaration
public class ApiKeyAuthProvider : AuthProvider, IAuthProvider, IAuthWithRequest, IAuthPlugin

Properties

Type

View Source
Declaration
public override string Type { get; }

ServiceRoutes

Modify the registration of GetApiKeys and RegenerateApiKeys Services

View Source
Declaration
public Dictionary<Type, string[]> ServiceRoutes { get; set; }

KeySizeBytes

How much entropy should the generated keys have. (default 24)

View Source
Declaration
public int KeySizeBytes { get; set; }

Environments

Generate different keys for different environments. (default live,test)

View Source
Declaration
public string[] Environments { get; set; }

KeyTypes

Different types of Keys each user can have. (default secret)

View Source
Declaration
public string[] KeyTypes { get; set; }

ExpireKeysAfter

Whether to automatically expire keys. (default no expiry)

View Source
Declaration
public TimeSpan? ExpireKeysAfter { get; set; }

InitSchema

Automatically create the ApiKey Table for AuthRepositories which need it. (default true)

View Source
Declaration
public bool InitSchema { get; set; }

RequireSecureConnection

Whether to only allow access via API Key from a secure connection. (default true)

View Source
Declaration
public bool RequireSecureConnection { get; set; }

GenerateApiKey

Change how API Key is generated

View Source
Declaration
public CreateApiKeyDelegate GenerateApiKey { get; set; }

CreateApiKeyFilter

Run custom filter after API Key is created

View Source
Declaration
public Action<ApiKey> CreateApiKeyFilter { get; set; }

SessionCacheDuration

Cache the User Session so it can be reused between subsequent API Key Requests

View Source
Declaration
public TimeSpan? SessionCacheDuration { get; set; }

AllowInHttpParams

Whether to allow API Keys in 'apikey' QueryString or FormData

View Source
Declaration
public bool AllowInHttpParams { get; set; }

Inherited Properties

AccountLockedValidator

View Source
Declaration
public Func<IAuthRepository, IUserAuth, IAuthTokens, bool> AccountLockedValidator { get; set; }

AuthEvents

View Source
Declaration
public IAuthEvents AuthEvents { get; }

AuthRealm

View Source
Declaration
public string AuthRealm { get; set; }

CallbackUrl

View Source
Declaration
public string CallbackUrl { get; set; }

CustomValidationFilter

View Source
Declaration
public Func<AuthContext, IHttpResult> CustomValidationFilter { get; set; }

ExcludeAuthInfoItems

View Source
Declaration
public HashSet<string> ExcludeAuthInfoItems { get; set; }

FormLayout

View Source
Declaration
public List<InputInfo> FormLayout { get; set; }

Icon

View Source
Declaration
public ImageInfo Icon { get; set; }

Label

View Source
Declaration
public string Label { get; set; }

LoadUserAuthFilter

View Source
Declaration
public Action<AuthUserSession, IAuthTokens, Dictionary<string, string>> LoadUserAuthFilter { get; set; }

LoadUserAuthInfoFilterAsync

View Source
Declaration
public Func<AuthUserSession, IAuthTokens, Dictionary<string, string>, CancellationToken, Task> LoadUserAuthInfoFilterAsync { get; set; }

Meta

View Source
Declaration
public virtual Dictionary<string, string> Meta { get; }

NavItem

View Source
Declaration
public NavItem NavItem { get; set; }

PersistSession

View Source
Declaration
public bool PersistSession { get; set; }

Provider

View Source
Declaration
public string Provider { get; set; }

RedirectUrl

View Source
Declaration
public string RedirectUrl { get; set; }

RestoreSessionFromState

View Source
Declaration
public bool? RestoreSessionFromState { get; set; }

SaveExtendedUserInfo

View Source
Declaration
public bool SaveExtendedUserInfo { get; set; }

SessionExpiry

View Source
Declaration
public TimeSpan? SessionExpiry { get; set; }

Sort

View Source
Declaration
public int Sort { get; set; }

Type

View Source
Declaration
public virtual string Type { get; }

Fields

Name

View Source
Declaration
public const string Name = "apikey"

Realm

View Source
Declaration
public const string Realm = "/auth/apikey"

DefaultTypes

View Source
Declaration
public static string[] DefaultTypes

DefaultEnvironments

View Source
Declaration
public static string[] DefaultEnvironments

DefaultKeySizeBytes

View Source
Declaration
public static int DefaultKeySizeBytes

Methods

Init(IAppSettings)

View Source
Declaration
protected virtual void Init(IAppSettings appSettings = null)
Parameters
TypeName
ServiceStack.Configuration.IAppSettingsappSettings

CreateApiKey(String, String, Int32)

View Source
Declaration
public virtual string CreateApiKey(string environment, string keyType, int sizeBytes)
Returns

System.String

Parameters
TypeName
System.Stringenvironment
System.StringkeyType
System.Int32sizeBytes

IsAuthorized(IAuthSession, IAuthTokens, Authenticate)

View Source
Declaration
public override bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
Returns

System.Boolean

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens
ServiceStack.Authenticaterequest

AuthenticateAsync(IServiceBase, IAuthSession, Authenticate, CancellationToken)

View Source
Declaration
public override async Task<object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Object>

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Authenticaterequest
System.Threading.CancellationTokentoken

PreAuthenticateAsync(IRequest, IResponse)

View Source
Declaration
public virtual async Task PreAuthenticateAsync(IRequest req, IResponse res)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.Web.IRequestreq
ServiceStack.Web.IResponseres

GetApiKeyAsync(IRequest, String)

View Source
Declaration
protected virtual async Task<ApiKey> GetApiKeyAsync(IRequest req, string apiKey)
Returns

System.Threading.Tasks.Task<ServiceStack.Auth.ApiKey>

Parameters
TypeName
ServiceStack.Web.IRequestreq
System.StringapiKey

ValidateApiKey(IRequest, ApiKey)

View Source
Declaration
public virtual void ValidateApiKey(IRequest req, ApiKey apiKey)
Parameters
TypeName
ServiceStack.Web.IRequestreq
ServiceStack.Auth.ApiKeyapiKey

PreAuthenticateWithApiKeyAsync(IRequest, IResponse, ApiKey)

View Source
Declaration
public virtual async Task PreAuthenticateWithApiKeyAsync(IRequest req, IResponse res, ApiKey apiKey)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.Web.IRequestreq
ServiceStack.Web.IResponseres
ServiceStack.Auth.ApiKeyapiKey

HasCachedSessionAsync(IRequest, String)

View Source
Declaration
public virtual async Task<bool> HasCachedSessionAsync(IRequest req, string apiSessionKey)
Returns

System.Threading.Tasks.Task<System.Boolean>

Parameters
TypeName
ServiceStack.Web.IRequestreq
System.StringapiSessionKey

CacheSessionAsync(IRequest, String)

View Source
Declaration
public virtual async Task CacheSessionAsync(IRequest req, string apiSessionKey)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.Web.IRequestreq
System.StringapiSessionKey

GetSessionKey(String)

View Source
Declaration
public static string GetSessionKey(string apiKey)
Returns

System.String

Parameters
TypeName
System.StringapiKey

Register(IAppHost, AuthFeature)

View Source
Declaration
public override void Register(IAppHost appHost, AuthFeature feature)
Parameters
TypeName
ServiceStack.IAppHostappHost
ServiceStack.AuthFeaturefeature

OnFailedAuthentication(IAuthSession, IRequest, IResponse)

View Source
Declaration
public override Task OnFailedAuthentication(IAuthSession session, IRequest httpReq, IResponse httpRes)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Web.IRequesthttpReq
ServiceStack.Web.IResponsehttpRes

GenerateNewApiKeys(String, String[])

View Source
Declaration
public List<ApiKey> GenerateNewApiKeys(string userId, params string[] environments)
Returns

System.Collections.Generic.List<ServiceStack.Auth.ApiKey>

Parameters
TypeName
System.StringuserId
System.String[]environments

Inherited Methods

AuthenticateAsync(IServiceBase, IAuthSession, Authenticate, CancellationToken)

View Source
Declaration
public abstract Task<object> AuthenticateAsync(IServiceBase authService, IAuthSession session, Authenticate request, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Object>

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Authenticaterequest
System.Threading.CancellationTokentoken

ConvertToClientError(Object, Boolean)

View Source
Declaration
protected virtual object ConvertToClientError(object failedResult, bool isHtml)
Returns

System.Object

Parameters
TypeName
System.ObjectfailedResult
System.BooleanisHtml

CreateAuthContext(IServiceBase, IAuthSession, IAuthTokens)

View Source
Declaration
protected virtual AuthContext CreateAuthContext(IServiceBase authService = null, IAuthSession session = null, IAuthTokens tokens = null)
Returns

ServiceStack.Auth.AuthContext

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens

CreateOrMergeAuthSession(IAuthSession, IAuthTokens)

View Source
Declaration
public virtual string CreateOrMergeAuthSession(IAuthSession session, IAuthTokens tokens)
Returns

System.String

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens

EmailAlreadyExistsAsync(IAuthRepositoryAsync, IUserAuth, IAuthTokens, CancellationToken)

View Source
Declaration
protected virtual async Task<bool> EmailAlreadyExistsAsync(IAuthRepositoryAsync authRepo, IUserAuth userAuth, IAuthTokens tokens = null, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Boolean>

Parameters
TypeName
ServiceStack.Auth.IAuthRepositoryAsyncauthRepo
ServiceStack.Auth.IUserAuthuserAuth
ServiceStack.Auth.IAuthTokenstokens
System.Threading.CancellationTokentoken

FallbackConfig(String)

Allows specifying a global fallback config that if exists is formatted with the Provider as the first arg. E.g. this appSetting with the TwitterAuthProvider: oauth.CallbackUrl="http://localhost:11001/auth/{0}" Would result in: oauth.CallbackUrl="http://localhost:11001/auth/twitter"

View Source
Declaration
protected string FallbackConfig(string fallback)
Returns

System.String

Parameters
TypeName
System.Stringfallback

GetAuthRedirectUrl(IServiceBase, IAuthSession)

View Source
Declaration
protected virtual string GetAuthRedirectUrl(IServiceBase authService, IAuthSession session)
Returns

System.String

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession

GetAuthRepository(IRequest)

View Source
Declaration
protected virtual IAuthRepository GetAuthRepository(IRequest req)
Returns

ServiceStack.Auth.IAuthRepository

Parameters
TypeName
ServiceStack.Web.IRequestreq

GetAuthRepositoryAsync(IRequest)

View Source
Declaration
protected virtual IAuthRepositoryAsync GetAuthRepositoryAsync(IRequest req)
Returns

ServiceStack.Auth.IAuthRepositoryAsync

Parameters
TypeName
ServiceStack.Web.IRequestreq

GetReferrerUrl(IServiceBase, IAuthSession, Authenticate)

View Source
Declaration
protected virtual string GetReferrerUrl(IServiceBase authService, IAuthSession session, Authenticate request = null)
Returns

System.String

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Authenticaterequest

GetUserAuthRepositoryAsync(IRequest)

View Source
Declaration
public IUserAuthRepositoryAsync GetUserAuthRepositoryAsync(IRequest request)
Returns

ServiceStack.Auth.IUserAuthRepositoryAsync

Parameters
TypeName
ServiceStack.Web.IRequestrequest

IsAccountLockedAsync(IAuthRepositoryAsync, IUserAuth, IAuthTokens, CancellationToken)

View Source
Declaration
public virtual Task<bool> IsAccountLockedAsync(IAuthRepositoryAsync authRepoAsync, IUserAuth userAuth, IAuthTokens tokens = null, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Boolean>

Parameters
TypeName
ServiceStack.Auth.IAuthRepositoryAsyncauthRepoAsync
ServiceStack.Auth.IUserAuthuserAuth
ServiceStack.Auth.IAuthTokenstokens
System.Threading.CancellationTokentoken

IsAuthorized(IAuthSession, IAuthTokens, Authenticate)

View Source
Declaration
public abstract bool IsAuthorized(IAuthSession session, IAuthTokens tokens, Authenticate request = null)
Returns

System.Boolean

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens
ServiceStack.Authenticaterequest

LoadUserAuthInfo(AuthUserSession, IAuthTokens, Dictionary<String, String>)

View Source
Declaration
[Obsolete("Use LoadUserAuthInfoAsync")]
protected void LoadUserAuthInfo(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo)
Parameters
TypeName
ServiceStack.AuthUserSessionuserSession
ServiceStack.Auth.IAuthTokenstokens
System.Collections.Generic.Dictionary<System.String,System.String>authInfo

LoadUserAuthInfoAsync(AuthUserSession, IAuthTokens, Dictionary<String, String>, CancellationToken)

View Source
Declaration
protected virtual Task LoadUserAuthInfoAsync(AuthUserSession userSession, IAuthTokens tokens, Dictionary<string, string> authInfo, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.AuthUserSessionuserSession
ServiceStack.Auth.IAuthTokenstokens
System.Collections.Generic.Dictionary<System.String,System.String>authInfo
System.Threading.CancellationTokentoken

LoginMatchesSession(IAuthSession, String)

View Source
Declaration
protected static bool LoginMatchesSession(IAuthSession session, string userName)
Returns

System.Boolean

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
System.StringuserName

LogoutAsync(IServiceBase, Authenticate, CancellationToken)

Remove the Users Session

View Source
Declaration
public virtual async Task<object> LogoutAsync(IServiceBase service, Authenticate request, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Object>

Parameters
TypeName
ServiceStack.IServiceBaseservice
ServiceStack.Authenticaterequest
System.Threading.CancellationTokentoken

OnAuthenticatedAsync(IServiceBase, IAuthSession, IAuthTokens, Dictionary<String, String>, CancellationToken)

View Source
Declaration
public virtual async Task<IHttpResult> OnAuthenticatedAsync(IServiceBase authService, IAuthSession session, IAuthTokens tokens, Dictionary<string, string> authInfo, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<ServiceStack.Web.IHttpResult>

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens
System.Collections.Generic.Dictionary<System.String,System.String>authInfo
System.Threading.CancellationTokentoken

OnFailedAuthentication(IAuthSession, IRequest, IResponse)

View Source
Declaration
public virtual Task OnFailedAuthentication(IAuthSession session, IRequest httpReq, IResponse httpRes)
Returns

System.Threading.Tasks.Task

Parameters
TypeName
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Web.IRequesthttpReq
ServiceStack.Web.IResponsehttpRes

Register(IAppHost, AuthFeature)

View Source
Declaration
public virtual void Register(IAppHost appHost, AuthFeature feature)
Parameters
TypeName
ServiceStack.IAppHostappHost
ServiceStack.AuthFeaturefeature

UrlFilter(AuthContext, String)

View Source
Declaration
public static string UrlFilter(AuthContext provider, string url)
Returns

System.String

Parameters
TypeName
ServiceStack.Auth.AuthContextprovider
System.Stringurl

UserNameAlreadyExistsAsync(IAuthRepositoryAsync, IUserAuth, IAuthTokens, CancellationToken)

View Source
Declaration
protected virtual async Task<bool> UserNameAlreadyExistsAsync(IAuthRepositoryAsync authRepo, IUserAuth userAuth, IAuthTokens tokens = null, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<System.Boolean>

Parameters
TypeName
ServiceStack.Auth.IAuthRepositoryAsyncauthRepo
ServiceStack.Auth.IUserAuthuserAuth
ServiceStack.Auth.IAuthTokenstokens
System.Threading.CancellationTokentoken

ValidateAccountAsync(IServiceBase, IAuthRepositoryAsync, IAuthSession, IAuthTokens, CancellationToken)

View Source
Declaration
protected virtual async Task<IHttpResult> ValidateAccountAsync(IServiceBase authService, IAuthRepositoryAsync authRepo, IAuthSession session, IAuthTokens tokens, CancellationToken token = default(CancellationToken))
Returns

System.Threading.Tasks.Task<ServiceStack.Web.IHttpResult>

Parameters
TypeName
ServiceStack.IServiceBaseauthService
ServiceStack.Auth.IAuthRepositoryAsyncauthRepo
ServiceStack.Auth.IAuthSessionsession
ServiceStack.Auth.IAuthTokenstokens
System.Threading.CancellationTokentoken

Implements