我必须自定义 UserManager
类以在公司结构中查找和验证用户(将 Active Directory 身份验证与另一个 Oracle 身份验证混合)。虽然我已经实现了 FindAsync
和 CreateIdentityAsync
,但用户未设置为经过身份验证。
我的 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。
关于c# - OWIN - 自定义 UserManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21785703/