我正在使用WWWForm接收来自Unity 3d项目的请求的应用程序:
public IEnumerator UpdateServerStats()
{
this.gameSessionStats.SessionUpdateTime = System.DateTime.Now;
while (true)
{
WWWForm frm = new WWWForm();
frm.AddField("GameSessionStatsId", System.Convert.ToString(this.gameSessionStats.GameSessionStatsId));
frm.AddField("Platform", System.Convert.ToString(this.gameSessionStats.Platform));
frm.AddField("GameKey", System.Convert.ToString( this.gameSessionStats.GameKey));
frm.AddField("SessionId", System.Convert.ToString(this.gameSessionStats.SessionId));
frm.AddField("SessionStartDateTime", System.Convert.ToString(this.gameSessionStats.SessionStartDateTime));
frm.AddField("SessionUpdateTime", System.Convert.ToString(this.gameSessionStats.SessionUpdateTime));
frm.AddField("UserName", System.Convert.ToString(this.gameSessionStats.UserName));
string strPostUrl = string.Format("{0}/api/GameStats", RestFulServer);
WWW clientWeb = new WWW(strPostUrl, frm.data);
yield return clientWeb;
if (clientWeb.error != null && clientWeb.error.Length > 0)
Debug.Log(clientWeb.error);
else
Debug.Log(clientWeb.text);
yield return new WaitForSeconds(5);
}
}
然后我有一个Web Api 2 Action
public HttpResponseMessage Post([FromBody]GameSessionStatsModel value)
{
PTIPortal.DA.Models.GameSessionStats newSessionStats = new DA.Models.GameSessionStats();
newSessionStats.GameSessionStatsId = value.GameSessionStatsId;
newSessionStats.Platform = value.Platform;
newSessionStats.GameKey = value.GameKey;
newSessionStats.SessionId = value.SessionId;
newSessionStats.SessionStartDateTime = value.SessionStartDateTime;
newSessionStats.SessionUpdateTime = value.SessionUpdateTime;
newSessionStats.UserName = value.UserName;
PTIPortal.DA.GameStatisticsDA gameStatsDA = new DA.GameStatisticsDA();
string result = gameStatsDA.CreateOrUpdateStatistics(newSessionStats);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, result);
return response;
}
一切正常,直到我在项目中启用Open Id身份验证, 即使我将[AllowAnonymous]设置为动作。 这是Startup.Auth.cs配置,如果我启用了行app.UseOpenIdConnectAuthentication,参数为null,如果我评论它的参数接收就好了。
app.UseOpenIdConnectAuthentication(
new OpenIdConnectAuthenticationOptions
{
ClientId = clientId,
Authority = Authority,
PostLogoutRedirectUri = postLogoutRedirectUri,
Notifications = new OpenIdConnectAuthenticationNotifications()
{
AuthenticationFailed = (context) =>
{
return Task.FromResult(0);
},
//
// If there is a code in the OpenID Connect response, redeem it for an access token and refresh token, and store those away.
//
AuthorizationCodeReceived = (context) =>
{
var code = context.Code;
ClientCredential credential = new ClientCredential(clientId, appKey);
string userObjectID = context.AuthenticationTicket.Identity.FindFirst(
"http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
AuthenticationContext authContext = new AuthenticationContext(Authority, new NaiveSessionCache(userObjectID));
AuthenticationResult result = authContext.AcquireTokenByAuthorizationCode(
code, new Uri(HttpContext.Current.Request.Url.GetLeftPart(UriPartial.Path)), credential, graphResourceId);
AuthenticationHelper.token = result.AccessToken;
return Task.FromResult(0);
},
MessageReceived = (context) =>
{
return Task.FromResult(0);
},
RedirectToIdentityProvider = (context) =>
{
return Task.FromResult(0);
},
SecurityTokenReceived = (context) =>
{
return Task.FromResult(0);
},
SecurityTokenValidated = (context) =>
{
return Task.FromResult(0);
}
}
});
在使用WWWForm构建请求时,或者在配置方面在服务器端构建请求时,我是否遗漏了某些内容?
感谢。