【问题标题】:Using Blazor OIDC authentication with Google OAuth only for Google Drive将 Blazor OIDC 身份验证与 Google OAuth 一起用于 Google Drive
【发布时间】:2021-03-26 18:16:37
【问题描述】:

在我的 Blazor WASM 应用程序中,我使用 OIDC 身份验证登录 Google Drive,如下所述:

Secure an ASP.NET Core Blazor WebAssembly standalone app with the Authentication library

Google Auth error getting access token in Blazor

Cannot log in or get access token with Google Authorization on Blazor WASM Standalone app

但我不想用

<AuthorizeRouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)">
    <NotAuthorized>
        @if (!context.User.Identity.IsAuthenticated)
        {
            <RedirectToLogin />
        }
    </NotAuthorized>
</AuthorizeRouteView>

因为我不想限制用户访问我的 Blazor 页面。

最重要的是:我不希望用户在未登录时看到“正在授权...”消息几秒钟 - 登录是可选的。

如果用户决定使用 Google Drive,我只需要 Google OAuth 登录,这样我就可以获得访问令牌。

如何仅使用 OIDC 身份验证来获取 Google Drive 的访问令牌?

如果这不可行,我可以使用 C# 登录到 Google,就像这里在 JavaScript 中看到的那样?

OAuth 2.0 for Client-side Web Applications

【问题讨论】:

    标签: google-oauth blazor openid-connect


    【解决方案1】:

    以下建议可能有效。如果没有,请报告更多问题,我会努力改进...

    在您的 App.razor 文件中进行以下更改:

    替换:

    <AuthorizeRouteView RouteData="@routeData" 
                                   DefaultLayout="@typeof(MainLayout)">
                <NotAuthorized>
                    @if (!context.User.Identity.IsAuthenticated)
                    {
                        <RedirectToLogin />
                    }
                    else
                    {
                        <p>You are not authorized to access this resource.</p>
                    }
                </NotAuthorized>
     </AuthorizeRouteView>
    

    <RouteView RouteData="@routeData" DefaultLayout="@typeof(MainLayout)" />
    

    您还必须删除所有添加的 Authorize 属性。

    您将不再使用RedirectToLogin 组件... 将LoginDisplay 组件保留在原位,以便在请求时启用身份验证。

    【讨论】:

    • 如果我使用 RouteView 而不是 AuthorizeRouteView 那么我可以选择显示 Google 登录页面,但登录后,应用程序会显示 /authentication/login-callback#state=...&access_token=...无法访问此站点 localhost 拒绝连接。 ERR_CONNECTION_REFUSED
    • 使用 RouteView 而不是 AuthorizeRouteView 有效,我的 OAuth 配置出现错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-29
    • 2014-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-01
    • 1970-01-01
    相关资源
    最近更新 更多