Skip to content

Commit

Permalink
Identity 3 - Google Login
Browse files Browse the repository at this point in the history
  • Loading branch information
XuanThuLab committed Sep 12, 2020
1 parent 2ba9611 commit 978d6fe
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 154 deletions.
7 changes: 3 additions & 4 deletions ASP_NET_CORE/Album/Album.csproj
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk.Web">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MailKit" Version="2.8.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity" Version="2.2.0" />
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.8" />
<PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.8" />
Expand All @@ -21,5 +21,4 @@
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
<PackageReference Include="System.Data.SqlClient" Version="4.8.2" />
</ItemGroup>

</Project>
</Project>
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
@page
@model ExternalLoginModel
@{
ViewData["Title"] = "Register";
ViewData["Title"] = "ĐĂNG KÝ TÀI KHOẢN";
}

<h1>@ViewData["Title"]</h1>
<h4 id="external-login-title">Associate your @Model.ProviderDisplayName account.</h4>
<h4 id="external-login-title">Thực hiện liên kết với tài khoản @Model.ProviderDisplayName.</h4>
<hr />

<p id="external-login-description" class="text-info">
You've successfully authenticated with <strong>@Model.ProviderDisplayName</strong>.
Please enter an email address for this site below and click the Register button to finish
logging in.
Bạn đã xác thực với tài khoản <strong>@Model.ProviderDisplayName</strong>.
Vui lòng nhập (hoặc xác nhận) chính xác email để liên kết và đăng nhập.
</p>

<div class="row">
Expand All @@ -23,7 +22,7 @@
<input asp-for="Input.Email" class="form-control" />
<span asp-validation-for="Input.Email" class="text-danger"></span>
</div>
<button type="submit" class="btn btn-primary">Register</button>
<button type="submit" class="btn btn-primary">Đăng ký</button>
</form>
</div>
</div>
Expand Down
245 changes: 156 additions & 89 deletions ASP_NET_CORE/Album/Areas/Identity/Pages/Account/ExternalLogin.cshtml.cs

Large diffs are not rendered by default.

46 changes: 19 additions & 27 deletions ASP_NET_CORE/Album/Areas/Identity/Pages/Account/Login.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -45,35 +45,27 @@
</form>
</section>
</div>

<div class="col-md-6 col-md-offset-2">
<section>
<h4>Đăng nhập bằng.</h4>
<hr />
@{
if ((Model.ExternalLogins?.Count ?? 0) == 0)
{
<div>
<p>
Chưa cu hình đăng nhp bng dch vkhác. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
</p>
</div>
}
else
{
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
}
}
</section>
@if ((Model.ExternalLogins?.Count ?? 0) != 0) {
<section>
<h4>Sdng dch v</h4>
<hr />
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
</section>
}
</div>


</div>

@section Scripts {
Expand Down
44 changes: 16 additions & 28 deletions ASP_NET_CORE/Album/Areas/Identity/Pages/Account/Register.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -36,34 +36,22 @@
</form>
</div>
<div class="col-md-6 col-md-offset-2">
<section>
<h4>Đăng ký bằng sử dụng các dịch vụ ngoài</h4>
<hr />
@{
if ((Model.ExternalLogins?.Count ?? 0) == 0)
{
<div>
<p>
Phn này chưa cu hình đăng ký tdch vngoài.
See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
</p>
</div>
}
else
{
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
}
}
</section>
@if ((Model.ExternalLogins?.Count ?? 0) != 0) {
<section>
<h4>Sdng dch v</h4>
<hr />
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
<div>
<p>
@foreach (var provider in Model.ExternalLogins)
{
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
}
</p>
</div>
</form>
</section>
}
</div>
</div>

Expand Down
19 changes: 19 additions & 0 deletions ASP_NET_CORE/Album/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,30 @@ public void ConfigureServices (IServiceCollection services) {
options.UseSqlServer (connectstring);
});



// Đăng ký Identity
services.AddIdentity<AppUser, IdentityRole> ()
.AddEntityFrameworkStores<AppDbContext> ()
.AddDefaultTokenProviders ();

services.AddAuthentication()
.AddGoogle(googleOptions =>
{
// Đọc thông tin Authentication:Google từ appsettings.json
IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
// Thiết lập ClientID và ClientSecret để truy cập API google
googleOptions.ClientId = googleAuthNSection["ClientId"];
googleOptions.ClientSecret = googleAuthNSection["ClientSecret"];
// Cấu hình Url callback lại từ Google (không thiết lập thì mặc định là /signin-google)
googleOptions.CallbackPath = "/dang-nhap-tu-google";
});




// Truy cập IdentityOptions
services.Configure<IdentityOptions> (options => {
// Thiết lập về Password
Expand Down
7 changes: 7 additions & 0 deletions ASP_NET_CORE/Album/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,12 @@
"Port": 587
},

"Authentication": {
"Google": {
"ClientId": "điền client id vào đây",
"ClientSecret": "điền secret vào đây"
}
},

"AllowedHosts": "*"
}
Binary file added imgs/identity-google01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google02.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google12.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added imgs/identity-google9.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 978d6fe

Please sign in to comment.