gpt4 book ai didi

c# - ASP.NET 身份 OWIN 中间件 Google OAuth2 AuthenticationManager 登录不工作

转载 作者:行者123 更新时间:2023-11-30 13:26:21 24 4
gpt4 key购买 nike

我创建了一个简单的 ASP.NET MVC4 网站来测试新的 OWIN 身份验证中间件,我决定从 Google OAuth2 开始,我在配置方面遇到了很多困难,但我设法让 Google 授权了用户,我现在遇到的问题是 OWIN 未对用户进行身份验证。

我想我在网络配置中有正确的设置

<system.web>
<authentication mode="None" />
</system.web>
<system.webServer>
<modules>
<remove name="FormsAuthenticationModule" />
</modules>
</system.webServer>

然后我在 Startup 类中有一个非常简单的配置

public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}

public void ConfigureAuth(IAppBuilder app)
{
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Enable the External Sign In Cookie.
app.SetDefaultSignInAsAuthenticationType(DefaultAuthenticationTypes.ExternalCookie);
// Enable Google authentication.
app.UseGoogleAuthentication(GetGoogleOptions());
}

private static GoogleOAuth2AuthenticationOptions GetGoogleOptions()
{
var reader = new KeyReader();
var keys = reader.GetKey("google");
var options = new GoogleOAuth2AuthenticationOptions()
{
ClientId = keys.Public,
ClientSecret = keys.Private
};
return options;
}
}

AccountController 中,我按照以下方式对操作进行了编码,这同样非常简单,但应该可行。

[AllowAnonymous, HttpPost, ValidateAntiForgeryToken]
public ActionResult ExternalLogin(string provider, string returnUrl)
{
return new ChallengeResult(provider, Url.Action("ExternalLoginCallback", "Account", new { ReturnUrl = returnUrl }));
}

[AllowAnonymous, HttpGet]
public async Task<ActionResult> ExternalLoginCallback(string returnUrl)
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null || !loginInfo.ExternalIdentity.IsAuthenticated)
{
return RedirectToAction("Login");
}

var identity = new ClaimsIdentity(new[] {
new Claim(ClaimTypes.Name, loginInfo.DefaultUserName),
new Claim(ClaimTypes.Email, loginInfo.Email)
}, DefaultAuthenticationTypes.ExternalCookie);

AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);

AuthenticationManager.SignIn(new AuthenticationProperties
{
IsPersistent = false
}, identity);

return RedirectToLocal(returnUrl);
}

我遇到的主要问题是调用方法 AuthenticationManager.SignIn 似乎没有做任何事情,即使 Google 授予访问请求的权限,当用户重定向到我有以下代码的主页

@using Microsoft.AspNet.Identity
@{
Layout = "~/Views/Shared/_Main.cshtml";
}
<h2>Welcome</h2>
@{
if (Request.IsAuthenticated)
{
<p>Welcome @User.Identity.GetUserName()</p>
}
else
{
@Html.ActionLink("Login", "Login", "Account")
}
}

Request.IsAuthenticated 的值始终为 false,有人知道我在这里缺少什么吗?根据我在网上阅读的内容,这应该有效。

我在我的浏览器中启用了 cookie,我拥有的其他 Google OAuth 示例依赖于 UserManager 类工作,但我的这个简单实现不起作用

最佳答案

在网上阅读了无数小时的答案后,我决定调试 OWIN 源代码以找到解决此问题的方法,而在调试 session 中我在 AuthenticationHandler 类中遇到了这个 gem

if (BaseOptions.AuthenticationMode == AuthenticationMode.Active)
{
AuthenticationTicket ticket = await AuthenticateAsync();
if (ticket != null && ticket.Identity != null)
{
Helper.AddUserIdentity(ticket.Identity);
}
}

在我原来的 Startup 类中,我使用这种方法启用了外部登录 cookie

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

此方法使用默认的 CookieAuthenticationOptions 实例,该实例具有 AuthenticationMode = AuthenticationMode.Passive,这会阻止类读取存储在 cookie 中的信息,这样OwinContext 未加载经过身份验证的身份的每个新请求都会导致 Request.IsAuthenticated

意识到这一点后,我所做的就是更改 app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie); 以此

app.UseCookieAuthentication(new CookieAuthenticationOptions()
{
AuthenticationMode = AuthenticationMode.Passive,
AuthenticationType = DefaultAuthenticationTypes.ExternalCookie,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
});

一切都很顺利

关于c# - ASP.NET 身份 OWIN 中间件 Google OAuth2 AuthenticationManager 登录不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25573345/

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