我正在嘗試處理 google-signin 到我現有的 ASP.NET MVC 應用程式。它有一個自定義開發的表單身份驗證,它適用于用戶名和密碼。
我想將 Google 登錄添加到此專案。我目前添加了按鈕,在 google 開發者控制臺中創建了應用程式,獲取了我的 ID 和密碼并設定了 url。
我可以在登錄頁面看到按鈕,點擊,我看到我的帳戶和圖片,選擇帳戶。此時谷歌向我發布了一些資料。我在 Action 方法中收到一個名為“credential”的字串陣列物件,它在第一個位置有一個字串。但我不知道從這里開始該怎么辦......
有人可以幫我解決這個問題嗎?我應該使用哪個檔案?
我正在閱讀:https : //developers.google.com/identity/gsi/web 直到現在,但我被卡住了。
我不想要的是:https : //docs.microsoft.com/en-us/aspnet/mvc/overview/security/create-an-aspnet-mvc-5-app-with-facebook-and-google-oauth2 -and-openid-登錄
我想自己處理所需的請求(使用 cookie、資料庫檢查和跟蹤令牌)并在我的控制器的操作方法中獲取谷歌自己提供的用戶資訊。
這是剃刀視圖代碼的一部分
<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 沒有安裝,也不會被使用(除非不使用它就不可能)。
-我的 .Net 框架版本:4.7.2
謝謝您的幫助。
uj5u.com熱心網友回復:
我也不使用“完整”默認身份驗證代碼,這是我處理 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是我自己的類,UserRole是自己的(結束為空)類,并且UserStoreService是我自己的實作IDisposable, IUserStore<User>, IUserEmailStore<User>, IUserClaimStore<User>, IUserSecurityStampStore<User>, IRoleStore<UserStoreService.UserRole>(您可以根據需要修改此串列)
uj5u.com熱心網友回復:
這是如何使用 owin 處理外部身份驗證的示例。首先,您必須設定您的 startup.cs 并使用類似的內容更新配置。當然,您必須將所需的包匯入到您的專案中。
public void Configuration(IAppBuilder app)
{
....
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
app.UseGoogleAuthentication(new GoogleOAuth2AuthenticationOptions()
{
ClientId = "YourGoogleClientId",
ClientSecret = "YourGoogleClientSecret",
});
}
然后在您的帳戶控制器(示例名稱)中創建方法,該方法將使用 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);
}
}
在您的帳戶控制器中添加您的新身份驗證方法。例如:
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; }
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401511.html
標籤:C# asp.net-mvc 谷歌oauth 谷歌登录 谷歌认证
