首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVC5网广告与数据库

问MVC5网广告与数据库
EN

Stack Overflow用户
提问于 2016-04-26 14:23:41
回答 2查看 665关注 0票数 2

我正在创建一个MVC5应用程序。这是一个内部网应用程序。所有用户都已通过本地Active域身份验证。

我们有一个现有的数据库,目前用于Windows应用程序。

我想获取用户的域名登录名,并使用它来查找已经在该数据库中配置的角色和声明。

我将假定ASP.NET / MVC5 / Authentication的基础项目“个人用户帐户”将是起点。

请给我指出正确的方向。

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-26 15:16:25

.NET OWIN标识类要求您通过ApplicationUserManager类的CheckPasswordAsync()方法进行身份验证。这可以通过重写类CheckPasswordAsync()方法来实现。在覆盖过程中,您需要调用类ValidateCredentials()方法,以便通过Active进行身份验证。这将要求用户使用他们的Windows用户名和密码登录到应用程序。不过,有几个步骤可以让它发挥作用。

正如您所说的,您从一个具有“个人用户帐户”身份验证的基本项目开始。

步骤1-通过将下面的代码添加到ConfigureAuth()方法来更新文件App_Start\Startup.Auth.cs中的ConfigureAuth()方法。

代码语言:javascript
复制
using System.DirectoryServices.AccountManagement;

//Add an Owin context for Active Directory principals
app.CreatePerOwinContext(() => new PrincipalContext(ContextType.Domain));

其余的更新在App_Start\IdentityConfig.cs文件中完成

步骤2-更新类ApplicationUserManager的构造函数。

代码语言:javascript
复制
using System.DirectoryServices.AccountManagement;

//Add a PrincipalContext parameter to the constructor
public ApplicationUserManager(IUserStore<ApplicationUser> store, PrincipalContext principal) : base(store)
{
    this.principal = principal;
}

步骤3-在Create()方法中,更新对类ApplicationUserManager构造函数的调用。

代码语言:javascript
复制
//Add the PrincipalContext parameter
var manager = new ApplicationUserManager(new UserStore<ApplicationUser>(context.Get<PortalIdentityDbContext>()), context.Get<PrincipalContext>());

步骤4-重写类ApplicationUserManager的ApplicationUserManager()方法。

代码语言:javascript
复制
//Override CheckPasswordAsync to login via Active Directory.
public override async Task<bool> CheckPasswordAsync(ApplicationUser user, string password)
{
    return await Task.FromResult(this.principal.ValidateCredentials(user.UserName, password, ContextOptions.Negotiate));
}

至于使用现有数据库,您必须在其中包含OWIN标识表,反之亦然。标识功能需要这些表,您不能更改这些表。我会创建一个测试项目并熟悉这些表。然后找出如何将它们合并到现有的数据库中,反之亦然。为了实现自定义功能,我对这些表进行了大量的修改。但核心表和列必须存在。

票数 1
EN

Stack Overflow用户

发布于 2016-04-26 15:16:34

您不需要整个ASP.Net标识。相反,您可以只使用OWIN身份验证中间件。

通过Active验证凭据

代码语言:javascript
复制
public bool ValidateCredentials(string userName, string password)
{
   using (var context = new PrincipalContext(ContextType.Domain))
   {
      return context.ValidateCredentials(userName, password);
   }
}

通过旧数据库授权

通过身份验证后,您希望从旧数据库中检索授权角色,并创建声明。

代码语言:javascript
复制
private readonly HttpContextBase _context;
private const string AuthenticationType = "ApplicationCookie";

public OwinAuthenticationService(HttpContextBase context)
{
    _context = context;
}

public void SignIn(User user)
{
    IList<Claim> claims = new List<Claim>
    {
        new Claim(ClaimTypes.Sid, user.Id.ToString()),
        new Claim(ClaimTypes.Name, user.UserName),
        new Claim(ClaimTypes.GivenName, user.FirstName),
        new Claim(ClaimTypes.Surname, user.LastName),
    };

    // Get authorized roles from old database
    foreach (Role role in user.Roles)
    {
        claims.Add(new Claim(ClaimTypes.Role, role.Name));
    }

    ClaimsIdentity identity = new ClaimsIdentity(claims, AuthenticationType);

    IOwinContext context = _context.Request.GetOwinContext();
    IAuthenticationManager authenticationManager = context.Authentication;

    authenticationManager.SignIn(identity);
}

public void SignOut()
{
    IOwinContext context = _context.Request.GetOwinContext();
    IAuthenticationManager authenticationManager = context.Authentication;

    authenticationManager.SignOut(AuthenticationType);
}

Startup.cs

您还需要配置启动,以使所有这些发生。

代码语言:javascript
复制
[assembly: OwinStartup(typeof(YourApplication.Startup))]
namespace YourApplication
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.UseCookieAuthentication(new CookieAuthenticationOptions
            {
                AuthenticationType = "ApplicationCookie",
                LoginPath = new PathString("/Account/Login")
            });
        }
    }
}

我希望你能得到起点。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36867784

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档