簡體   English   中英

OWIN-自定義UserManager

[英]OWIN - Customizing UserManager

我必須自定義UserManager類以查找和驗證公司結構中的用戶(將Active Directory身份驗證與另一個Oracle身份驗證混合在一起)。 盡管我已經實現了FindAsyncCreateIdentityAsync ,但用戶未設置為已認證。

我的UserManager實施:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Security.Claims;
using System.Web;
using MyProject.Common;
using MyProject.Models;
using Microsoft.AspNet.Identity;
using System.Threading.Tasks;

namespace MyProject.Infrastructure
{
    public class GNUserManager : UserManager<ApplicationUser>
    {
        public GNUserManager(IUserStore<ApplicationUser> store) : base(store)
        {

        }        

        public override async Task<ApplicationUser> FindAsync(string userName, string password)
        {
            /* Performs some logic here that returns true */

            if (foundUser) {
                return await Task.Run(() => new ApplicationUser
                {
                    UserName = userName, 
                    Id = userName
                });
            }

            throw new Exception("User not found.");
        }

        public override async Task<ClaimsIdentity> CreateIdentityAsync(ApplicationUser user, string authenticationType)
        {
            IList<Claim> claimCollection = new List<Claim>
            {
                new Claim(ClaimTypes.Name, user.UserName),
                new Claim(ClaimTypes.Country, "Brazil"),
                new Claim(ClaimTypes.Email, user.UserName)
            };

            var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");

            return await Task.Run(() => claimsIdentity);  
        }
    }
}

我的用戶沒有得到什么認證?

嘗試更改此行。

 var claimsIdentity = new ClaimsIdentity(claimCollection, "Company Portal");

對此

var claimsIdentity = new ClaimsIdentity(claimCollection, DefaultAuthenticationTypes.ApplicationCookie);

那應該為您生成所需的cookie。

UserManager管理數據庫中的用戶身份以及驗證憑據。 簡而言之,它是一個數據庫查找工具。 要使用戶“登錄”您的應用程序,您需要發出某種令牌(例如,用於瀏覽器應用程序的cookie或用於api應用程序的令牌)。 ASP.NET中的最新方法是針對瀏覽器應用程序的Cookie身份驗證中間件。 有關cookie中間件的更多信息,請參見此處:

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

查看由ASP.NET MVC 5默認項目創建的SignIn方法,我們可以看到以下代碼:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
    var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
    AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}

我們可以注意到的是, AuthenticationManager是負責維護登錄的人,在我們獲得身份后,也需要使用AuthenticationManager登錄。 因此,也許您的問題不在於UserManager

通過以下代碼檢索Controller類中的AuthenticationManager實例:

private IAuthenticationManager AuthenticationManager
{
    get
    {
        return HttpContext.GetOwinContext().Authentication;
    }
}

Oracle Data Provider for .NET當前不支持異步查詢和保存。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM