在 Xamarin.Forms 上,使用 Microsoft.Identity.Client nuGet 包,我尝试运行 Oauth2.0 和 openID 流程。我从这个文档官方页面开始: https://learn.microsoft.com/en-us/azure/active-directory/develop/v2-protocols-oidc
唯一的问题,查询参数response_type应该是: 响应类型=代码+id_token
但是库将其修复为: 响应类型=代码
其他一切都工作正常。关于如何编辑参数有什么想法吗?
我尝试使用以下方法作弊:
.WithExtraQueryParameters(new Dictionary<string, string>(){{ "response_type","code+id_token"} }) // error duplicated url key
但会引发重复键错误
您可以查看官方文档关于xamarin表单中的身份验证和授权。
而官方文件说:
IdentityServer 4 是用于 ASP.NET Core 的开源 OpenID Connect 和 OAuth 2.0 框架,可用于许多身份验证和授权场景,包括为本地 ASP.NET Core Identity 用户颁发安全令牌。
您可以通过以下代码登录:
public string CreateAuthorizationRequest()
{
// Create URI to authorization endpoint
var authorizeRequest = new AuthorizeRequest(GlobalSetting.Instance.IdentityEndpoint);
// Dictionary with values for the authorize request
var dic = new Dictionary<string, string>();
dic.Add("client_id", GlobalSetting.Instance.ClientId);
dic.Add("client_secret", GlobalSetting.Instance.ClientSecret);
dic.Add("response_type", "code id_token");
dic.Add("scope", "openid profile basket orders locations marketing offline_access");
dic.Add("redirect_uri", GlobalSetting.Instance.Callback);
dic.Add("nonce", Guid.NewGuid().ToString("N"));
dic.Add("code_challenge", CreateCodeChallenge());
dic.Add("code_challenge_method", "S256");
// Add CSRF token to protect against cross-site request forgery attacks.
var currentCSRFToken = Guid.NewGuid().ToString("N");
dic.Add("state", currentCSRFToken);
var authorizeUri = authorizeRequest.Create(dic);
return authorizeUri;
}