Reputation: 103
System.UnauthorizedAccessException is triggered in our Xamarin iOS app when reading a shared file, either from another app or through a public folder.
This error is new and started after updating from VS2019 to VS2022 (Windows). There were also a number of macOS updates (now Ventura 13.7). The sharing code works fine but there seems to be a security setting missing - hopefuly a simple oversight.
Below are snippets from the app. Any suggestions on where to start looking for the cause?
info.plist:
<dict>
<key>CFBundleTypeName</key>
<string>Public plain-text</string>
<key>LSItemContentTypes</key>
<array>
<string>public.plain-text</string>
</array>
</dict>
AppDelegate.cs:
public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
{
MyModule mm = new MyModule();
mm.AddFile(url.Path);
return true;
}
MyModule.cs:
public class MyModule
{
public void AddFile(string url)
{
...
var text = File.ReadAllText(url); << Exception line >>
...
}
}
Exception message:
{System.UnauthorizedAccessException: Access to the path "/private/var/mobile/Containers/Shared/AppGroup/XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX/File Provider Storage/xxxxx.txt" is denied.
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x0026e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.IO/FileStream.cs:274
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/mcs/class/corlib/System.IO/FileStream.cs:106
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks, System.Int32 bufferSize) [0x00055] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/IO/StreamReader.cs:182
at System.IO.StreamReader..ctor (System.String path, System.Text.Encoding encoding, System.Boolean detectEncodingFromByteOrderMarks) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/Common/src/CoreLib/System/IO/StreamReader.cs:167
at System.IO.File.InternalReadAllText (System.String path, System.Text.Encoding encoding) [0x00000] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:303
at System.IO.File.ReadAllText (System.String path) [0x00026] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.IO.FileSystem/src/System/IO/File.cs:282
at [App].MyModule.AddFile (System.String fullPath) [0x00002] in C:\xxxx\...\xxxx.cs:187 }
Upvotes: 0
Views: 36