ClaimsPrincipal.Current.Identity.Name空从客户端身份验证时,罚款浏览器
问题描述:
我有以下天青功能,ClaimsPrincipal.Current.Identity.Name空从客户端身份验证时,罚款浏览器
#r "Newtonsoft.Json"
using Newtonsoft.Json.Linq;
using System.Net;
using System.Security.Claims;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
try
{
JObject pJOtClaims = new JObject();
foreach(Claim curClaim in ClaimsPrincipal.Current.Identities.First().Claims)
{
pJOtClaims.Add(curClaim.Type, new JValue(curClaim.Value));
}
return(req.CreateResponse(HttpStatusCode.OK, $"{pJOtClaims.ToString(Newtonsoft.Json.Formatting.None)}"));
}
catch(Exception ex)
{
return(req.CreateResponse(HttpStatusCode.OK, $"{ex.Message}"));
}
}
我对这个功能的应用程序仅配置Facebook验证。此功能适用于浏览器内和客户端身份验证。当我在浏览器中调用此方法时,我收到了一大堆索赔,包括我注册的Facebook电子邮件地址。当我调用这个来自客户端身份验证,我得到了下面的权利要求,
{
"stable_sid":"...",
"http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier":"...",
"http://schemas.microsoft.com/identity/claims/identityprovider":"...",
"ver":"...",
"iss":"...",
"aud":"...",
"exp":"...",
"nbf":"..."
}
可惜这些都不包括我需要我的Facebook的电子邮件地址。我已经为Facebook认证配置启用了“电子邮件”范围。任何想法如何得到这个?
Nick。
答
好吧,所以我还没有找到我想要的确切解决方案,但这应该让我受益匪浅。从技术上讲,我只需要注册时的电子邮件地址,之后我可以使用stable_sid作为我获得身份的一部分。所以我所做的就是将x-zumo-auth头文件传递给“.auth/me”方法,获得我需要的属性。我用这个方法
public static async Task<String> GetAuthProviderParam(String iAuthMeURL,
String iXZumoAUth,
String iParamKey)
{
using (HttpClient pHCtClient = new HttpClient())
{
pHCtClient.DefaultRequestHeaders.Add("x-zumo-auth", iXZumoAUth);
String pStrResponse = await pHCtClient.GetStringAsync(iAuthMeURL);
JObject pJOtResponse = JObject.Parse(pStrResponse.Trim(new Char[] { '[', ']' }));
if(pJOtResponse[iParamKey] != null)
{
return (pJOtResponse[iParamKey].Value<String>());
}
else
{
throw new KeyNotFoundException(String.Format("A parameter with the key '{0}' was not found.", iParamKey));
}
}
}
这可以在像这样的函数调用,
if(req.Headers.Contains("x-zumo-auth"))
{
String pStrXZumoAuth = req.Headers.GetValues("x-zumo-auth").First();
String pStrParam = await FunctionsHelpers.GetAuthProviderParam("https://appname.azurewebsites.net/.auth/me",
pStrXZumoAuth,
"user_id");
//pStrParam = user_id
}
我还要指出,在CurrentPrincipal.Current.Identities枚举只包含一个身份。 – Nick