Reactjs 使用.NET核心Web API和React进行Active Directory身份验证
我不知道我是否只是在寻找正确的位置,但我似乎找不到正确的指南,说明从哪里开始使用React/.NET Core 2.1 Web API和(基于prem的)Active Directory身份验证 一般来说,我对.NET身份验证比较陌生,对Active Directory身份验证完全陌生 我开始使用.NETCore2.1React模板并尝试向其中添加auth,但完全迷失了方向Reactjs 使用.NET核心Web API和React进行Active Directory身份验证,reactjs,authentication,active-directory,.net-core,ldap,Reactjs,Authentication,Active Directory,.net Core,Ldap,我不知道我是否只是在寻找正确的位置,但我似乎找不到正确的指南,说明从哪里开始使用React/.NET Core 2.1 Web API和(基于prem的)Active Directory身份验证 一般来说,我对.NET身份验证比较陌生,对Active Directory身份验证完全陌生 我开始使用.NETCore2.1React模板并尝试向其中添加auth,但完全迷失了方向 我从哪里开始呢?对我来说,第一步是设置JWT身份验证 接下来,我必须找到一个库,用于根据Active Directory检
我从哪里开始呢?对我来说,第一步是设置JWT身份验证 接下来,我必须找到一个库,用于根据Active Directory检查用户。(可用于.NET Core) 现在,我必须创建一个带有
[AllowAnonymous]
属性的新控制器。我将其命名为LoginController
,并创建了一个如下所示的操作:
[AllowAnonymous]
[HttpPost]
// Notice: We get a custom request object from the body
public async Task<IActionResult> Login([FromBody] AuthRequest request)
{
// Create a context that will allow you to connect to your Domain Controller
using (var adContext = new PrincipalContext(ContextType.Domain, "mydomain.com"))
{
var result = adContext.ValidateCredentials(request.username, request.password);
if (result)
{
// Create a list of claims that we will add to the token.
// This is how you can control authorization.
var claims = new[]
{
// Get the user's Name (this can be whatever claims you wish)
new Claim(ClaimTypes.Name, request.username)
};
// Read our custom key string into a a usable key object
var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration.GetSection("SOME_TOKEN").Value));
// create some signing credentials using out key
var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
// create a JWT
var token = new JwtSecurityToken(
issuer: "mydomain.com",
audience: "mydomain.com",
claims: claims, // the claims listed above
expires: DateTime.Now.AddMinutes(30), // how long you wish the token to be active for
signingCredentials: creds);
Since we return an IActionResult, wrap the token inside of a status code 200 (OK)
return Ok(new
{
token = new JwtSecurityTokenHandler().WriteToken(token)
});
}
}
}
}
// if we haven't returned by now, something went wrong and the user is not authorized
return Unauthorized();
}
现在,在我的React应用程序中,我所要做的就是向LoginController
发出一个简单的获取请求,并使用我可以从登录表单获得的用户用户名和密码。结果将是一个JWT,我可以保存到state(但应该保存到cookies:使其变得微不足道)
现在,您可以将[Authorize]
属性添加到.NET核心应用程序中的任何控制器,并在从React客户端传递JWT时向其发出获取请求,如下所示:
public class AuthRequest
{
public string username { get; set; }
public string password { get; set; }
}
await fetch(`someController/someAction`,
{
method: 'GET'
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${YOUR_JWT}`
}
})
.then(response => doSomething());
如果希望将此JWT与信号器一起使用,请在.NET核心项目中将[Authorize]
属性添加到Hub
。然后,在React客户端中,当您实例化到集线器的连接时:
import * as signalR from '@aspnet/signalr';
var connection = new signalR.HubConnectionBuilder().withUrl('myHub', { accessTokenFactory: () => YOUR_JWT })
还有,中提琴能够进行授权实时通信的.NET核心React应用程序
import * as signalR from '@aspnet/signalr';
var connection = new signalR.HubConnectionBuilder().withUrl('myHub', { accessTokenFactory: () => YOUR_JWT })