C# 在外部验证后,如何从用户收集更多信息?

C# 在外部验证后,如何从用户收集更多信息?,c#,asp.net-core,google-authentication,C#,Asp.net Core,Google Authentication,我使用的是Microsoft.AspNetCore.Authentication.Google软件包。如果我允许用户通过Google认证,我应该在哪里插入我自己的表单来收集Google无法访问的更多信息(例如自定义标识符) 在他们授权登录时,我是否应该提交一份表单,预先收集数据并将其存储在会话或其他内容中 我是否应该让他们授权登录,然后在调用回调URL时,在那里显示表单 通过中间件公开了四个事件: OnTicketReceived OnCreatingTicket on重定向到授权端点 OnRe

我使用的是
Microsoft.AspNetCore.Authentication.Google
软件包。如果我允许用户通过Google认证,我应该在哪里插入我自己的表单来收集Google无法访问的更多信息(例如自定义标识符)

  • 在他们授权登录时,我是否应该提交一份表单,预先收集数据并将其存储在会话或其他内容中

  • 我是否应该让他们授权登录,然后在调用回调URL时,在那里显示表单

  • 通过中间件公开了四个事件:

  • OnTicketReceived
  • OnCreatingTicket
  • on重定向到授权端点
  • OnRemoteFailure

  • 在任何地方都有这样的例子吗?我好像什么也找不到。

    我用Cookie中间件完成了。我添加了'temp'cookie中间件来捕获登录到Google的ClaimsPrincipal,然后我登录到'real'cookie中间件来保存丰富的ClaimsPrincipal。StartUp类的Configure方法中的相关代码段:

            app.UseCookieAuthentication(
            new CookieAuthenticationOptions()
            {
                AuthenticationScheme = "Cookie",
                AutomaticAuthenticate = true,
                AutomaticChallenge = true,
                LoginPath = new PathString(@"/account/login"),
                AccessDeniedPath = new PathString(@"/account/accessdenied")
            });
    
        app.UseCookieAuthentication(
            new CookieAuthenticationOptions()
            {
                AuthenticationScheme = "Temp",
                AutomaticAuthenticate = false
            });
    
        var googleOptions = new GoogleOptions()
        {
            AuthenticationScheme = "Google",
            SignInScheme = "Temp",
            AppId = "yourappidhere",
            AppSecret = "yourappsecrethere"
        };
        googleOptions.Scope.Add("scopesyouneed");
    
        app.UseGoogleAuthentication(googleOptions);
    
    注意googleOptions的符号主题是“Temp”,而“Temp”Cookie中间件的选项将其自动验证为false(因为您不想自动将ClaimsPrinciple保留在Temp Cookie中,而是在真正的Cookie(此处称为“Cookie”)中进行充实和完善)

    那么控制器中的相关方法如下所示:

        public async Task<IActionResult> Register(string returnUrl = null)
    {
        var externalPrincipal = await HttpContext.Authentication.AuthenticateAsync("Temp");
    
        //TODO Check external principal and retrieve claims from db or whatever needs to be done here.
    
        var claims = new List<Claim>()
            {
                new Claim("email", externalPrincipal.FindFirst(ClaimTypes.Email).Value)
            };
        var id = new ClaimsIdentity(claims, "password");
        await HttpContext.Authentication.SignInAsync("Cookie", new ClaimsPrincipal(id));
        await HttpContext.Authentication.SignOutAsync("Temp");
    
        return Redirect(returnUrl);
    }
    
    public async Task<IActionResult> LogInGoogle(string returnUrl = null)
    {
        var queryString = !string.IsNullOrWhiteSpace(returnUrl) ? $"?returnUrl={returnUrl}" : string.Empty;
        var props = new AuthenticationProperties() { RedirectUri = $@"Account/Register{queryString}" }; //new PathString(returnUrl)
    
        return await Task.Run<ChallengeResult>(() => new ChallengeResult("Google", props));
    
    }
    
    public异步任务寄存器(string returnUrl=null)
    {
    var externalPrincipal=await HttpContext.Authentication.authenticateSync(“Temp”);
    //TODO检查外部主体并从db中检索索赔或需要在此处执行的任何操作。
    var索赔=新列表()
    {
    新索赔(“电子邮件”,externalPrincipal.FindFirst(ClaimTypes.email).Value)
    };
    var id=新的索赔实体(索赔,“密码”);
    等待HttpContext.Authentication.SignInAsync(“Cookie”,新的ClaimsPrincipal(id));
    等待HttpContext.Authentication.SignOutAsync(“Temp”);
    返回重定向(returnUrl);
    }
    公共异步任务登录日志(字符串returnUrl=null)
    {
    var queryString=!string.IsNullOrWhiteSpace(returnUrl)?$“?returnUrl={returnUrl}”:string.Empty;
    var props=new AuthenticationProperties(){RedirectUri=$@“帐户/寄存器{queryString}”};//新路径字符串(returnUrl)
    返回等待任务。运行(()=>newchallengeresult(“Google”,props));
    }
    
    请注意如何通过页面上的链接或其他方式调用LoginLogle。还记得谷歌中间件在这一点上的意义是“临时”吗。它被重定向到“注册”操作方法。在这里,您可以使用以下代码从Google中提取ClaimsPrinciple:

    var externalPrincipal=await HttpContext.Authentication.authenticateSync(“Temp”)

    在这一点上,你可以做任何你需要做的索赔。如您所见,我提取了电子邮件声明。我使用我的“Cookie”登录方案登录,以将ClaimsPrinciple持久化到Cookie中。但是您也可以使用表单重定向到视图,您可以使用表单向用户请求更多信息