Skip to content

Commit fe3068b

Browse files
committed
Add external logins to register page
1 parent 152cc3a commit fe3068b

File tree

5 files changed

+94
-5
lines changed

5 files changed

+94
-5
lines changed

src/Identity/UI/src/Areas/Identity/Pages/V3/Account/Register.cshtml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@
3030
<button type="submit" class="btn btn-default">Register</button>
3131
</form>
3232
</div>
33+
<div class="col-md-6 col-md-offset-2">
34+
<section>
35+
<h4>Use another service to register.</h4>
36+
<hr />
37+
@{
38+
if ((Model.ExternalLogins?.Count ?? 0) == 0)
39+
{
40+
<div>
41+
<p>
42+
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
43+
for details on setting up this ASP.NET application to support logging in via external services.
44+
</p>
45+
</div>
46+
}
47+
else
48+
{
49+
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
50+
<div>
51+
<p>
52+
@foreach (var provider in Model.ExternalLogins)
53+
{
54+
<button type="submit" class="btn btn-default" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
55+
}
56+
</p>
57+
</div>
58+
</form>
59+
}
60+
}
61+
</section>
62+
</div>
3363
</div>
3464

3565
@section Scripts {

src/Identity/UI/src/Areas/Identity/Pages/V3/Account/Register.cshtml.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.ComponentModel.DataAnnotations;
7+
using System.Linq;
68
using System.Text.Encodings.Web;
79
using System.Threading;
810
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Authentication;
912
using Microsoft.AspNetCore.Authorization;
1013
using Microsoft.AspNetCore.Identity.UI.Services;
1114
using Microsoft.AspNetCore.Mvc;
@@ -36,6 +39,12 @@ public abstract class RegisterModel : PageModel
3639
/// </summary>
3740
public string ReturnUrl { get; set; }
3841

42+
/// <summary>
43+
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
44+
/// directly from your code. This API may change or be removed in future releases.
45+
/// </summary>
46+
public IList<AuthenticationScheme> ExternalLogins { get; set; }
47+
3948
/// <summary>
4049
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
4150
/// directly from your code. This API may change or be removed in future releases.
@@ -75,7 +84,7 @@ public class InputModel
7584
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
7685
/// directly from your code. This API may change or be removed in future releases.
7786
/// </summary>
78-
public virtual void OnGet(string returnUrl = null) => throw new NotImplementedException();
87+
public virtual Task OnGetAsync(string returnUrl = null) => throw new NotImplementedException();
7988

8089
/// <summary>
8190
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
@@ -108,14 +117,16 @@ public RegisterModel(
108117
_emailSender = emailSender;
109118
}
110119

111-
public override void OnGet(string returnUrl = null)
120+
public override async Task OnGetAsync(string returnUrl = null)
112121
{
113122
ReturnUrl = returnUrl;
123+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
114124
}
115125

116126
public override async Task<IActionResult> OnPostAsync(string returnUrl = null)
117127
{
118128
returnUrl = returnUrl ?? Url.Content("~/");
129+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
119130
if (ModelState.IsValid)
120131
{
121132
var user = CreateUser();

src/Identity/UI/src/Areas/Identity/Pages/V4/Account/Register.cshtml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,36 @@
3030
<button type="submit" class="btn btn-primary">Register</button>
3131
</form>
3232
</div>
33+
<div class="col-md-6 col-md-offset-2">
34+
<section>
35+
<h4>Use another service to register.</h4>
36+
<hr />
37+
@{
38+
if ((Model.ExternalLogins?.Count ?? 0) == 0)
39+
{
40+
<div>
41+
<p>
42+
There are no external authentication services configured. See <a href="https://go.microsoft.com/fwlink/?LinkID=532715">this article</a>
43+
for details on setting up this ASP.NET application to support logging in via external services.
44+
</p>
45+
</div>
46+
}
47+
else
48+
{
49+
<form id="external-account" asp-page="./ExternalLogin" asp-route-returnUrl="@Model.ReturnUrl" method="post" class="form-horizontal">
50+
<div>
51+
<p>
52+
@foreach (var provider in Model.ExternalLogins)
53+
{
54+
<button type="submit" class="btn btn-primary" name="provider" value="@provider.Name" title="Log in using your @provider.DisplayName account">@provider.DisplayName</button>
55+
}
56+
</p>
57+
</div>
58+
</form>
59+
}
60+
}
61+
</section>
62+
</div>
3363
</div>
3464

3565
@section Scripts {

src/Identity/UI/src/Areas/Identity/Pages/V4/Account/Register.cshtml.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.ComponentModel.DataAnnotations;
7+
using System.Linq;
68
using System.Text.Encodings.Web;
79
using System.Threading;
810
using System.Threading.Tasks;
11+
using Microsoft.AspNetCore.Authentication;
912
using Microsoft.AspNetCore.Authorization;
1013
using Microsoft.AspNetCore.Identity.UI.Services;
1114
using Microsoft.AspNetCore.Mvc;
@@ -35,6 +38,12 @@ public abstract class RegisterModel : PageModel
3538
/// </summary>
3639
public string ReturnUrl { get; set; }
3740

41+
/// <summary>
42+
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
43+
/// directly from your code. This API may change or be removed in future releases.
44+
/// </summary>
45+
public IList<AuthenticationScheme> ExternalLogins { get; set; }
46+
3847
/// <summary>
3948
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
4049
/// directly from your code. This API may change or be removed in future releases.
@@ -74,7 +83,7 @@ public class InputModel
7483
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
7584
/// directly from your code. This API may change or be removed in future releases.
7685
/// </summary>
77-
public virtual void OnGet(string returnUrl = null) => throw new NotImplementedException();
86+
public virtual Task OnGetAsync(string returnUrl = null) => throw new NotImplementedException();
7887

7988
/// <summary>
8089
/// This API supports the ASP.NET Core Identity default UI infrastructure and is not intended to be used
@@ -107,14 +116,16 @@ public RegisterModel(
107116
_emailSender = emailSender;
108117
}
109118

110-
public override void OnGet(string returnUrl = null)
119+
public override async Task OnGetAsync(string returnUrl = null)
111120
{
112121
ReturnUrl = returnUrl;
122+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
113123
}
114124

115125
public override async Task<IActionResult> OnPostAsync(string returnUrl = null)
116126
{
117127
returnUrl = returnUrl ?? Url.Content("~/");
128+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
118129
if (ModelState.IsValid)
119130
{
120131
var user = CreateUser();

src/Identity/samples/IdentitySample.DefaultUI/Areas/Identity/Pages/Account/Register.cshtml.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Collections.Generic;
56
using System.ComponentModel.DataAnnotations;
7+
using System.Linq;
68
using System.Text.Encodings.Web;
79
using System.Threading.Tasks;
810
using IdentitySample.DefaultUI.Data;
11+
using Microsoft.AspNetCore.Authentication;
912
using Microsoft.AspNetCore.Identity;
1013
using Microsoft.AspNetCore.Identity.UI.Services;
1114
using Microsoft.AspNetCore.Mvc;
@@ -38,6 +41,8 @@ public RegisterModel(
3841

3942
public string ReturnUrl { get; set; }
4043

44+
public IList<AuthenticationScheme> ExternalLogins { get; set; }
45+
4146
public class InputModel
4247
{
4348
[Required]
@@ -67,14 +72,16 @@ public class InputModel
6772
public int Age { get; set; }
6873
}
6974

70-
public void OnGet(string returnUrl = null)
75+
public async Task OnGetAsync(string returnUrl = null)
7176
{
7277
ReturnUrl = returnUrl;
78+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
7379
}
7480

7581
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
7682
{
7783
returnUrl = returnUrl ?? Url.Content("~/");
84+
ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
7885
if (ModelState.IsValid)
7986
{
8087
var user = new ApplicationUser {

0 commit comments

Comments
 (0)