Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9b9eb05

Browse files
Get the userid in controller class
1 parent 48256a8 commit 9b9eb05

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

‎Webgentle.BookStore/Webgentle.BookStore/Controllers/HomeController.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Webgentle.BookStore.Models;
99
using Webgentle.BookStore.Repository;
10+
using Webgentle.BookStore.Service;
1011

1112
namespace Webgentle.BookStore.Controllers
1213
{
@@ -15,18 +16,26 @@ public class HomeController : Controller
1516
private readonly NewBookAlertConfig _newBookAlertconfiguration;
1617
private readonly NewBookAlertConfig _thirdPartyBookconfiguration;
1718
private readonly IMessageRepository _messageRepository;
19+
private readonly IUserService _userService;
1820

19-
public HomeController(IOptionsSnapshot<NewBookAlertConfig> newBookAlertconfiguration, IMessageRepository messageRepository)
21+
public HomeController(IOptionsSnapshot<NewBookAlertConfig> newBookAlertconfiguration,
22+
IMessageRepository messageRepository,
23+
IUserService userService)
2024
{
2125
_newBookAlertconfiguration = newBookAlertconfiguration.Get("InternalBook");
2226
_thirdPartyBookconfiguration = newBookAlertconfiguration.Get("ThirdPartyBook");
2327
_messageRepository = messageRepository;
28+
_userService = userService;
2429
}
2530

2631
public ViewResult Index()
2732
{
28-
bool isDisplay = _newBookAlertconfiguration.DisplayNewBookAlert;
29-
bool isDisplay1 = _thirdPartyBookconfiguration.DisplayNewBookAlert;
33+
34+
//var userId = _userService.GetUserId();
35+
//var isLoggedIn = _userService.IsAuthenticated();
36+
37+
//bool isDisplay = _newBookAlertconfiguration.DisplayNewBookAlert;
38+
//bool isDisplay1 = _thirdPartyBookconfiguration.DisplayNewBookAlert;
3039

3140
//var value = _messageRepository.GetName();
3241

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Webgentle.BookStore.Service
2+
{
3+
public interface IUserService
4+
{
5+
string GetUserId();
6+
bool IsAuthenticated();
7+
}
8+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Microsoft.AspNetCore.Http;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Security.Claims;
6+
using System.Threading.Tasks;
7+
8+
namespace Webgentle.BookStore.Service
9+
{
10+
public class UserService : IUserService
11+
{
12+
private readonly IHttpContextAccessor _httpContext;
13+
14+
public UserService(IHttpContextAccessor httpContext)
15+
{
16+
_httpContext = httpContext;
17+
}
18+
public string GetUserId()
19+
{
20+
return _httpContext.HttpContext.User?.FindFirstValue(ClaimTypes.NameIdentifier);
21+
}
22+
23+
public bool IsAuthenticated()
24+
{
25+
return _httpContext.HttpContext.User.Identity.IsAuthenticated;
26+
}
27+
}
28+
}

‎Webgentle.BookStore/Webgentle.BookStore/Startup.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
using Webgentle.BookStore.Helpers;
1717
using Webgentle.BookStore.Models;
1818
using Webgentle.BookStore.Repository;
19-
19+
using Webgentle.BookStore.Service;
20+
2021
namespace Webgentle.BookStore
2122
{
2223
public class Startup
@@ -67,6 +68,7 @@ public void ConfigureServices(IServiceCollection services)
6768
services.AddScoped<ILanguageRepository, LanguageRepository>();
6869
services.AddSingleton<IMessageRepository, MessageRepository>();
6970
services.AddScoped<IAccountRepository, AccountRepository>();
71+
services.AddScoped<IUserService, UserService>();
7072

7173
services.AddScoped<IUserClaimsPrincipalFactory<ApplicationUser>, ApplicationUserClaimsPrincipalFactory>();
7274

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /