Microsoft.ASPNET.Identityプロバイダを使用していて、カスタムプリンシパルを設定したいとします。私が代わりにアイデンティティと校長と一緒に遊んのientityプロバイダASP.NET IDプロバイダのカスタムプリンシパルを設定する
答えて
と似た何かを行うことができ、私はどうしたらどのように
protected void Application_AuthenticateRequest(Object sender, EventArgs e)
{
HttpCookie authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
if(authCookie != null)
{
using (var db = new GSCM.Logic.GSCMDatabase())
{
FormsAuthenticationTicket ticket = FormsAuthentication.Decrypt(authCookie.Value);
var id = new GenericIdentity(ticket.Name);
var principal = new VmUserLogin(id);
var found = db.LoadInternalUser(ticket.Name);
if(found != null)
{
Mapper.Map(found, principal);
}
HttpContext.Current.User = principal;
}
}
}
:以前FomrsAuthenticationで、私はこのような何かをやっているだろう
次
は、アプリケーションで必要なユーザー情報のためのインタフェースを作成します
public interface ICurrentUserService
{
VmUserLogin CurrentUser{get; }
}
だけ注入し、あなたのコントローラ内部で、で最後ICurrentUserServiceあなたはIoCコンテナを使用する必要が
public class MyController : Controller
{
private readonly ICurrentUserService _currentUserService;
public MyController(ICurrentUserService currentUserService)
{
_currentUserService = currentUserService;
}
public ActionResult Index()
{
return View(_currentUserService.CurrentUser);
}
}
を
public class HttpLoggedInUserService : ICurrentUserService
{
private readonly IUserRepository _userRepository;
private VmUserLogin _currentUser;
public HttpLoggedInUserService(IUserRepository userRepository)
{
_userRepository= userRepository;
}
public VmUserLogin CurrentUser
{
get
{
if (_currentUser == null)
{
if (HttpContext.Current.Items.Contains("CurrentUser"))
return HttpContext.Current.Items["CurrentUser"] as VmUserLogin ;
var loginName = HttpContext.Current.User.Identity.Name.ToLower();
_currentUser = _userRepository.GetByLoginName(loginName);
HttpContext.Current.Items["CurrentUser"] = _currentUser ;
}
if (_currentUser == null)
{
HttpContext.Current.Response.Redirect("/NoAccess");
return null;
}
return _currentUser ;
}
}
}
以下のようにWebプロジェクト内でこのインタフェースの実装を作成し、このソリューション必要に応じてコントローラ、ビジネス層、さらにはデータアクセス層に現在ログインしているユーザーを注入することができます。これらの層のどれもがどのように取得されたかを知ることはできません
をロードする必要はありませんが、それを使用したいすべてのアクションで呼び出す必要があります – dagda1
データベースからユーザーを一度取得し、キャッシュされます要求のライフサイクル中の残りのコールについては、クッキーを使用したい場合やセッション中に保存することもできます。 –
- 1. パートとIDプロバイダとの間でADFSを設定する
- 2. ASP.Net WebAPIアプリケーション用のASP.Net IDの設定
- 3. Asp.Net IDでredirect_uriを設定する
- 4. ASP.NET 5 MVC 6 NoSQL IDプロバイダを実装する方法
- 5. asp.netのMVC3のエディタフォースのヘルパーのIDを設定する方法
- 6. Thinktecture IdentityServer 2.0でIDプロバイダになるようにADFSを設定する
- 7. ローカルのDynamoDBをASP.NETセッション状態プロバイダとして設定する方法
- 8. Java OAuthプロバイダの設定
- 9. SAMのIDプロバイダ
- 10. 複数のIDプロバイダのログインOAuth/Open ID
- 11. ワークフローのカスタムプリンシパルを使用
- 12. asp.net - マスターページフォームIDを動的に設定する
- 13. asp.netでajax.Actionlinkにidを動的に設定する方法は?
- 14. OAuth 2.0のIDプロバイダ
- 15. キークローキングIDプロバイダのマッパーを定義する方法は?
- 16. ASP.NET Core&OpenID Connect外部IDプロバイダへのリダイレクト
- 17. 複数のIDプロバイダをPingFederate
- 18. ASP.NETコアのEventLogプロバイダ
- 19. asp.net SQLメンバーシップ・プロバイダのIDからロールを取得する方法は?
- 20. サンプルIDプロバイダSAML 2.0
- 21. ASP.NETキャッシュコントロールヘッダーを設定する
- 22. カスタム設定プロバイダでカスタムクラスをシリアライズ
- 23. カスタム設定プロバイダとデフォルト値の取得
- 24. APIをIDプロバイダにログインする方法
- 25. 複数の外部IDプロバイダを使用するIDサーバ4
- 26. ASP.netの設定
- 27. SAML:プロバイダIDの問題
- 28. Keycloak - SAMLプロバイダのカスタムエンティティID?
- 29. Firebase認証 - プロバイダのID
- 30. OpenAM/OpenSSOのカスタムIDプロバイダ
なぜ必要なのですか?それをするには? –
だから私は各コントローラアクション内のオブジェクトにアクセスし、毎回 – dagda1