Browse Source

Use CanConvertFrom to determine whether string keys can be converted to TKey to prevent FormatException exceptions

pull/111/head
Kévin Chalet 10 years ago
parent
commit
008a908ec6
  1. 10
      src/OpenIddict.EF/OpenIddictStore.cs

10
src/OpenIddict.EF/OpenIddictStore.cs

@ -26,7 +26,15 @@ namespace OpenIddict {
}
public virtual Task<TApplication> FindApplicationByIdAsync(string identifier, CancellationToken cancellationToken) {
var key = (TKey) TypeDescriptor.GetConverter(typeof(TKey)).ConvertFromInvariantString(identifier);
var converter = TypeDescriptor.GetConverter(typeof(TKey));
// If the string key cannot be converted to TKey, return null
// to indicate that the requested application doesn't exist.
if (!converter.CanConvertFrom(typeof(string))) {
return Task.FromResult<TApplication>(null);
}
var key = (TKey) converter.ConvertFromInvariantString(identifier);
return Applications.SingleOrDefaultAsync(application => application.Id.Equals(key), cancellationToken);
}

Loading…
Cancel
Save