c# - 更改 Google 默认身份验证重定向 - C#(Google 库)

标签 c# asp.net-mvc-4 google-api google-api-dotnet-client

我试试这个代码:https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth#web_applications

我的代码相同

但是!我需要更改此默认 url 重定向。 现在是:redirect_uri=http:%2F%2Flocalhost:52674%2FAuthCallback%2FIndexAsync

如何更改此网址?伙计们请帮忙。

谢谢

最佳答案

我还发现一开始更改 Google 的 OAuth 2.0 重定向 uri 真的很棘手,但事实证明这很简单。您可以通过不同的方式执行此操作。如果您遵循 Google 的 OAuth 2.0 Web 应用程序 (ASP.NET MVC) 指南,最简单的选择是覆盖 AppFlowMetadata 类中的 AuthCallback 字符串。

https://developers.google.com/api-client-library/dotnet/guide/aaa_oauth

public override string AuthCallback
{
    get { return @"/AuthCallback/Index"; }
}

您也可以实现您自己的“AuthorizationCodeMvcApp”版本,但这太过分了。不要过溪取水。 :)

https://github.com/google/google-api-dotnet-client/tree/master/Src/GoogleApis.Auth.Mvc4/OAuth2/Mvc

但是如果你想这样做,这里有一个例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Mvc;
using Google.Apis.Auth.OAuth2.Mvc;
using Google.Apis.Auth.OAuth2.Web;

namespace ProjectName.GoogleCalendar
{
        /// <summary>
        /// Thread-safe OAuth 2.0 authorization code flow for a MVC web application that persists end-user credentials.
        /// </summary>
        public class CustomAuthorizationCodeMvcApp : AuthorizationCodeWebApp
        {

            private readonly Controller controller;
            private readonly FlowMetadata flowData;

            /// <summary>Gets the controller which is the owner of this authorization code MVC app instance.</summary>
            public Controller Controller { get { return controller; } }

            /// <summary>Gets the <see cref="Google.Apis.Auth.OAuth2.Mvc.FlowMetadata"/> object.</summary>
            public FlowMetadata FlowData { get { return flowData; } }

            /// <summary>Constructs a new authorization code MVC app using the given controller and flow data.</summary>
            public CustomAuthorizationCodeMvcApp(Controller controller, FlowMetadata flowData)
                : base(
                flowData.Flow,
                new Uri(controller.Request.Url.GetLeftPart(UriPartial.Authority) + "/CustomController" + flowData.AuthCallback).ToString(),
                controller.Request.Url.ToString())
            {
                this.controller = controller;
                this.flowData = flowData;
            }

            /// <summary>
            /// Asynchronously authorizes the installed application to access user's protected data. It gets the user 
            /// identifier by calling to <see cref="Google.Apis.Auth.OAuth2.Mvc.FlowMetadata.GetUserId"/> and then calls to
            /// <see cref="Google.Apis.Auth.OAuth2.AuthorizationCodeWebApp.AuthorizeAsync"/>.
            /// </summary>
            /// <param name="taskCancellationToken">Cancellation token to cancel an operation</param>
            /// <returns>
            /// Auth result object which contains the user's credential or redirect URI for the authorization server
            /// </returns>
            public Task<AuthResult> AuthorizeAsync(CancellationToken taskCancellationToken)
            {
                return base.AuthorizeAsync(FlowData.GetUserId(Controller), taskCancellationToken);
            }
        }
    }

关于c# - 更改 Google 默认身份验证重定向 - C#(Google 库),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27090804/

相关文章:

c# - Web API [FromBody] 参数在发送对象数组时为空

javascript - KendoUI 网格不会触发 saveChanges 事件

c# - 使用数据注释进行验证

c# - C# 中的 C++ "system()"

c# - 为使用 ExcelPackage 创建的 Excel 单元格设置货币格式

c# - 提交按钮没有按预期工作

asp.net-mvc - angularjs 仅带有 cshtml 页面,而不是带有 web api 2 的 html 页面

android - Google drive android API 无法检索文件内容

javascript - Google 脚本中的数组覆盖问题

javascript - 监控 Google 电子表格单元格值并撤消不需要的更改