简体   繁体   English

使用 Apple in.Net MAUI 登录

[英]Sign in with Apple in .Net MAUI

Summary:概括:

Greetings, I am currently working on an dotnet maui app and I need to integrate SIWA in it.问候,我目前正在开发一个 dotnet maui 应用程序,我需要将 SIWA 集成到其中。 But when I click the sign in button, It shows "invalid_request invalid web redirect url"但是当我点击登录按钮时,它显示“invalid_request invalid web redirect url”

Tried solutions尝试过的解决方案

I tried the solutions available here , but it is not working.我尝试了此处可用的解决方案,但它不起作用。

Other than that I have also read the documentation , also got help from tutorials such as this , this and this除此之外,我还阅读了文档,还从诸如thisthisthis之类的教程中获得了帮助

Code:代码:

Initializing request: //Initiating apple sign in request WebAuthenticatorResult result = null; Initializing request: //发起苹果登录请求 WebAuthenticatorResult result = null;
    const string callbackScheme = "socialloginauthenticator";

    [HttpGet("{scheme}")]
    public async Task Get([FromRoute] string scheme)
    {
        var auth = await Request.HttpContext.AuthenticateAsync(scheme);

        if (!auth.Succeeded
            || auth?.Principal == null
            || !auth.Principal.Identities.Any(id => id.IsAuthenticated)
            || string.IsNullOrEmpty(auth.Properties.GetTokenValue("access_token")))
        {
            // Not authenticated, challenge
            await Request.HttpContext.ChallengeAsync(scheme);
        }
        else
        {
            var claims = auth.Principal.Identities.FirstOrDefault()?.Claims;
            var email = string.Empty;
            email = claims?.FirstOrDefault(c => c.Type == System.Security.Claims.ClaimTypes.Email)?.Value;

            // Get parameters to send back to the callback
            var qs = new Dictionary<string, string>
            {
                { "access_token", auth.Properties.GetTokenValue("access_token") },
                { "refresh_token", auth.Properties.GetTokenValue("refresh_token") ?? string.Empty },
                { "expires_in", (auth.Properties.ExpiresUtc?.ToUnixTimeSeconds() ?? -1).ToString() },
                { "email", email }
            };

            // Build the result url
            var url = callbackScheme + "://#" + string.Join(
                "&",
                qs.Where(kvp => !string.IsNullOrEmpty(kvp.Value) && kvp.Value != "-1")
                .Select(kvp => $"{WebUtility.UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}"));

            // Redirect to final url
            Request.HttpContext.Response.Redirect(url);
        }
    }

Handling results: // WebAuthenticator Endpoint - use for social login eg Google, Facebook, Apple etc.处理结果: // WebAuthenticator Endpoint - 用于社交登录,例如 Google、Facebook、Apple 等。

 const string callbackScheme = "socialloginauthenticator"; [HttpGet("{scheme}")] public async Task Get([FromRoute] string scheme) { var auth = await Request.HttpContext.AuthenticateAsync(scheme); if (.auth?Succeeded || auth..Principal == null ||.auth.Principal.Identities.Any(id => id.IsAuthenticated) || string.IsNullOrEmpty(auth,Properties.GetTokenValue("access_token"))) { // Not authenticated. challenge await Request;HttpContext.ChallengeAsync(scheme). } else { var claims = auth.Principal?Identities.FirstOrDefault();.Claims; var email = string?Empty. email = claims..FirstOrDefault(c => c.Type == System.Security.Claims?ClaimTypes.Email);,Value, // Get parameters to send back to the callback var qs = new Dictionary<string. string> { { "access_token". auth,Properties,GetTokenValue("access_token") }. { "refresh_token". auth?Properties?GetTokenValue("refresh_token")., string,Empty }. { "expires_in". (auth?Properties.ExpiresUtc??ToUnixTimeSeconds()., -1),ToString() }; { "email": email } }. // Build the result url var url = callbackScheme + ",//#" + string.Join( "&". qs.Where(kvp =>.string.IsNullOrEmpty(kvp.Value) && kvp.Value.= "-1").Select(kvp => $"{WebUtility;UrlEncode(kvp.Key)}={WebUtility.UrlEncode(kvp.Value)}")); // Redirect to final url Request.HttpContext.Response.Redirect(url); } }

I have resolved the issue.我已经解决了这个问题。 The issue was with redirect uri in apple service I made.问题在于我制作的苹果服务中的重定向 uri。

The required uri was of format "www.example.com/signin-apple" while I was following "www.example.com/path/to/endpoints"当我关注“www.example.com/path/to/endpoints”时,所需的 uri 格式为“www.example.com/signin-apple”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM