gpt4 book ai didi

c# - ASP.NET WEB API 外部登录

转载 作者:太空宇宙 更新时间:2023-11-03 12:37:10 25 4
gpt4 key购买 nike

我使用 Visual Studio 2013 项目向导在 ASP.NET 中创建 WEB API 项目。它为社交登录创建了这个功能:

// GET api/Account/ExternalLogin
[OverrideAuthentication]
[HostAuthentication(DefaultAuthenticationTypes.ExternalCookie)]
[AllowAnonymous]
[Route("ExternalLogin", Name = "ExternalLogin")]
public async Task<IHttpActionResult> GetExternalLogin(string provider, string error = null)
{
if (error != null)
{
return Redirect(Url.Content("~/") + "#error=" + Uri.EscapeDataString(error));
}

if (!User.Identity.IsAuthenticated)
{
return new ChallengeResult(provider, this);
}

ExternalLoginData externalLogin = ExternalLoginData.FromIdentity(User.Identity as ClaimsIdentity);

if (externalLogin == null)
{
return InternalServerError();
}

if (externalLogin.LoginProvider != provider)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
return new ChallengeResult(provider, this);
}

ApplicationUser user = await UserManager.FindAsync(new UserLoginInfo(externalLogin.LoginProvider,
externalLogin.ProviderKey));

bool hasRegistered = user != null;

if (hasRegistered)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);

ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(UserManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = await user.GenerateUserIdentityAsync(UserManager,
CookieAuthenticationDefaults.AuthenticationType);

AuthenticationProperties properties = ApplicationOAuthProvider.CreateProperties(user.UserName);
Authentication.SignIn(properties, oAuthIdentity, cookieIdentity);
}
else
{
IEnumerable<Claim> claims = externalLogin.GetClaims();
ClaimsIdentity identity = new ClaimsIdentity(claims, OAuthDefaults.AuthenticationType);
Authentication.SignIn(identity);
}

return Ok();
}

然后我用C#写了一个客户端代码来调用这个函数:

public async Task LogInAsync(string url, string provider)
{
using (HttpClient client = new HttpClient())
{
string request = url + "/api/Account/ExternalLogin";
var query = HttpUtility.ParseQueryString(string.Empty);
query["provider"] = provider;
query["error"] = "";
request += "?" + query.ToString();

HttpResponseMessage responseMessage = await client.GetAsync(request);
if (responseMessage.IsSuccessStatusCode)
{
string responseContent = await responseMessage.Content.ReadAsStringAsync();
}
}
}

奇怪的是,我从服务器收到了这个错误响应:

StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Pragma: no-cache
X-SourceFiles: =?UTF-8?B?RTpcUHJvamVjdFxEYXRpbmdcRGF0aW5nLlNlcnZlclxhcGlcQWNjb3VudFxFeHRlcm5hbExvZ2lu?= Cache-Control: no-cache Date: Tue, 08 Nov 2016 15:12:33 GMT
Server: Microsoft-IIS/8.0 X-Powered-By: ASP.NET Content-Length: 24 Content-Type: text/plain; charset=UTF-8 Expires: -1 }

当我尝试在 Web 浏览器中导航相应链接时出现相同的错误。在服务器调试中,未命中此函数的相应入口点。我做错了什么?它是 GET 动词,所以我应该能够以任何一种方式成功访问它。

最让我困惑的是,这个函数默认包含在每个 WEB API 项目中,但我找不到任何引用资料或提及人们如何在实践中使用它。

最佳答案

默认情况下,OAuthServerOptions 具有以下内容:

AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin")

只需删除该行,您的 ExternalLOgin 端点就会正常工作。

文件:Startup.Auth.cs

关于c# - ASP.NET WEB API 外部登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40490526/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com