Для меня первым шагом было настроить аутентификацию JWT , , как описано в этом блоге MSDN .
Затем мне пришлось найти библиотеку, чтобы использовать ее для проверки пользователя в Active Directory . Я выбрал System.DirectoryServices.AccountManagement (доступно для .NET Core).
Теперь мне пришлось создать новый контроллер с [AllowAnonymous] attribute. I called it
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();
}
Объект AuthRequest
может выглядеть примерно так:
public class AuthRequest
{
public string username { get; set; }
public string password { get; set; }
}
Теперь, в моем приложении React все, что мне нужно сделать, это сделать простой запрос на LoginController
с именем пользователя и паролем пользователя, которые я могу получить из формы входа в систему. Результатом будет JWT, который я могу сохранить в состояние (но следует сохранить в куки: react-cookie
библиотека делает это тривиальным).
fetch(`login`, {
method: "POST",
headers: {
'content-type': 'application/json',
'accept': 'application/json',
},
body: JSON.stringify({this.state.username, this.state.password})
}).then((response) => {
if (response.status === 401) {
// handle the 401 gracefully if this user is not authorized
}
else {
// we got a 200 and a valid token
response.json().then(({ token }) => {
// handle saving the token to state/a cookie
})
}
})
Теперь у вас есть возможность добавить атрибут [Authorize]
к любому из ваших контроллеров в приложении .NET Core и сделать запрос на выборку к нему при передаче вашего JWT от вашего клиента React, вот так:
await fetch(`someController/someAction`,
{
method: 'GET'
headers: {
'content-type': 'application/json',
'authorization': `Bearer ${YOUR_JWT}`
}
})
.then(response => doSomething());
Если вы хотите использовать этот JWT с SignalR Hub
, добавьте атрибут [Authorize]
к вашему Hub
в вашем проекте .NET Core. Затем, в вашем клиенте React, когда вы создаете соединение с вашим концентратором:
import * as signalR from '@aspnet/signalr';
var connection = new signalR.HubConnectionBuilder().withUrl('myHub', { accessTokenFactory: () => YOUR_JWT })
И, альт! Приложение .NET Core React с возможностью авторизованного общения в реальном времени!