![](/img/trans.png)
[英]Google Sign-in in ASP.Net MVC Application to use Gmail API
[英]Google Sign-in with existing ASP.NET MVC Application
我正在尝试将 google-signin 处理到我现有的 ASP.NET MVC 应用程序。 它具有自定义开发的 forms 身份验证,并且适用于用户名和密码。
我想在这个项目中添加 Google 登录。 我目前添加了按钮,在谷歌开发者控制台中创建了应用程序,获取了我的 ID 和密码并设置了网址。
我可以在登录页面看到按钮,点击,我看到我的账户和图片,select 账户。 此时谷歌向我发布了一些数据。 我在 Action 方法中收到了一个名为“credential”的字符串数组 object,它在第一个 position 中有一个字符串。 但我不知道从这里开始该怎么办......
有人可以帮我吗? 我应该使用哪个文件?
我正在读这个: https://developers.google.com/identity/gsi/web直到现在,但我被困住了。
我想自己处理所需的请求(使用 cookies,数据库检查和跟踪令牌)并在我的控制器的操作方法中获取谷歌自己提供的用户信息。
这里是razor查看代码的一部分
<div id="g_id_onload"
data-client_id="my id is here"
data-login_uri="@Url.Action("LoginWithGoogle","Login")"
data-auto_prompt="false">
</div>
<div class="g_id_signin"
data-type="standard"
data-size="large"
data-theme="outline"
data-text="sign_in_with"
data-shape="rectangular"
data-logo_alignment="left"
data-auto_prompt="true"
>
</div>
我添加了这个脚本:
<script src="https://accounts.google.com/gsi/client" async defer></script>
这个 Action 方法可以捕获字符串:
[HttpPost]
public ActionResult LoginWithGoogle(string[] credential)
{
ViewBag.Data = credential;
///I will do necessary stuff here.
return View();
}
笔记:
-Identity 未安装且不会被使用(除非不可能不使用它)。
-My.Net 框架版本:4.7.2
谢谢您的帮助。
我也不使用“完整”默认身份验证代码,这是我处理 Google/Yandex/Discord/OAuth 响应的方式:
[HttpPost]
[AllowAnonymous]
public IActionResult ExternalLogin(string provider, string? returnUrl = null)
{
var redirectUrl = Url.Action(nameof(ExternalLoginCallback), null, new { returnUrl });
var properties = signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
return new ChallengeResult(provider, properties);
}
[HttpGet]
[AllowAnonymous]
public async Task<IActionResult> ExternalLoginCallback(string? returnUrl = null)
{
var info = await signInManager.GetExternalLoginInfoAsync();
if (info == null)
{
return this.Redirect(LoginPath);
}
var email = info.Principal.FindFirstValue(ClaimTypes.Email);
var name = info.Principal.FindFirstValue(ClaimTypes.Name) ?? info.Principal.Identity.Name;
// your own actions & checks with email, name etc
这仍然需要在 Startup 中进行一些“默认”准备:
services.AddIdentity<User, UserStoreService.UserRole>()
.AddUserStore<UserStoreService>()
.AddRoleStore<UserStoreService>()
.AddDefaultTokenProviders();
services.AddAuthentication().AddGoogle(...)
但是这里User
是我自己的class, UserRole
是自己的(结尾为空)class, UserStoreService
是我自己实现的IDisposable, IUserStore<User>, IUserEmailStore<User>, IUserClaimStore<User>, IUserSecurityStampStore<User>, IRoleStore<UserStoreService.UserRole>
(您可以根据需要修改此列表)
这是如何使用 owin 处理外部身份验证的示例。 首先,您必须设置您的 startup.cs 并使用类似的内容更新配置。 当然,您必须将所需的包导入到您的项目中。
public void Configuration(IAppBuilder app)
{
....
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "YourGoogleClientId",
ClientSecret = "YourGoogleClientSecret",
});
}
然后在您的帐户 controller(示例名称)中创建将使用 google 按钮处理您的登录的方法。 例如:
public ActionResult ExternalLogin(string provider)
{
var returnUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/ExternalLoginCallback";
return new ChallengeResult(provider, returnUrl);
}
创建您的 ChallengeResult 方法:
internal class ChallengeResult : HttpUnauthorizedResult
{
public ChallengeResult(string provider, string redirectUrl)
{
LoginProvider = provider;
RedirectUri = redirectUrl;
}
public string LoginProvider { get; set; }
public string RedirectUri { get; set; }
public override void ExecuteResult(ControllerContext context)
{
var properties = new AuthenticationProperties { RedirectUri = RedirectUri };
var owin = context.HttpContext.GetOwinContext();
owin.Authentication.Challenge(properties, LoginProvider);
}
}
在您的帐户 controller 中添加新的身份验证方法。 例如:
public async Task<ActionResult> ExternalLoginCallback()
{
try
{
var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();
if (loginInfo == null || string.IsNullOrEmpty(loginInfo.Email))
{
return RedirectToAction("ExternalLoginFailed", "WebAccount");
}
else
{
// handle external login, means process user login in your system
}
}
catch (Exception ex)
{
// handle possible exceptions
}
return RedirectToAction("ExternalLoginFailed", "WebAccount");
}
private IAuthenticationManager AuthenticationManager
{
get { return HttpContext.GetOwinContext().Authentication; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.