.NET4中的Google OAuth 2.0无法在中型信任级别服务器上创建ServiceAccount

时间:2014-11-30 19:35:48

标签: c# google-calendar-api google-oauth medium-trust service-accounts

我想阅读.NET4.0 Web服务中的一些日历事件。因为没有涉及用户,所以我使用了服务帐户授权方法。 它在我的本地机器上完美运行。但是,当我想在托管服务器上安装它时,我在创建证书期间获得了权限异常。原因是Web服务的中等信任级别。但是这个级别在托管服务器上很常见。 是否有其他方法可以访问可在中等信任级别服务器上运行的API?

这是我的内部服务代码:

public static List<CalendarEventObject> GetEvents( string calendarName, int maxToGet, out string error )
    {
        string done = "";
        error = "";

        try
        {
            // access google api with service account
            String serviceAccountEmail = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx@developer.gserviceaccount.com";

            var p12File = System.Web.HttpContext.Current.Server.MapPath( "./bin/MyAppsServiceAccountFileFromGoogleDeveoperConsole.p12" );

            done += "maped certificate: " + p12File;

            // this needs System.Security.Permissions.KeyContainerPermission permission, 
            // that is not inside medium trust (nor inside high trust)
            var certificate = new X509Certificate2( p12File
                , "notasecret"
                , X509KeyStorageFlags.MachineKeySet |
                  X509KeyStorageFlags.PersistKeySet |
                  X509KeyStorageFlags.Exportable );         // tried several storage flags with no success            

            ServiceAccountCredential credential = new ServiceAccountCredential(
            new ServiceAccountCredential.Initializer( serviceAccountEmail )
            {
                Scopes = new[] { CalendarService.Scope.Calendar }
            }.FromCertificate( certificate ) );

            // Create the service.
            var service = new CalendarService( new BaseClientService.Initializer( )
            {
                HttpClientInitializer = credential,
                ApplicationName = "MyCalendarReader",
            } );

            done += "service created ";

            // Fetch the list of calendar list
            var list = service.CalendarList.List( ).Execute( ).Items;

            done += "CalendarList loaded ";

            if ( list.Count == 0 ) throw new Exception( "CalendarList returned none" );

            bool found = false;
            List<string> calendars = new List<string>( );
            foreach ( var calendarListEntry in list )
            {
                calendars.Add( calendarListEntry.Summary );
                if ( calendarListEntry.Summary == calendarName )
                {
                    found = true;
                    return ExtractEvents( service, calendarListEntry, maxToGet );
                }
            }

            if ( !found )
            {
                throw new Exception( "No matching calendar: " + String.Join( ";", calendars ) );
            }
        }
        catch ( Exception ex )
        {
            error = String.Format( "\nDone:{0}\n msg:{1}\n inner:{2}\n stack:{3}", done, ex.Message, ex.InnerException != null ? ex.InnerException.Message : "none", ex.StackTrace );
        }

        return new List<CalendarEventObject>( );
    }

1 个答案:

答案 0 :(得分:0)

除了更改信任级别之外,我认为除了更改信任级别之外,还有解决问题的方法。

&#34;中等信任级别在托管服务器上很常见&#34;声明,虽然它曾经是真的 - 它不再存在。

{p} Medium Trust已由ASP.NET小组过时。主要原因是在过去几年中反射开始被更多地使用。在Medium Trust中进行反思几乎是不可能的。

您的最佳解决方案,特别是从长远来看,是告诉您的托管服务提供商,并在他们拒绝向您提供完全信任的情况下找到其他提供商。

PS:ASP.NET Partial Trust does not guarantee application isolation