我正在使用JWT安全令牌和自定义身份验证方案在我的 Web 应用程序中进行身份验证。
我在用户登录时生成令牌
我创建了一个身份验证处理程序,在其中验证所有请求的令牌
身份验证处理程序
public class CustomAuthenticationHandler : AuthenticationHandler<CustomAuthenticationOptions>
{
public CustomAuthenticationHandler(
IOptionsMonitor<CustomAuthenticationOptions> options,
ILoggerFactory logger,
UrlEncoder encoder,
ISystemClock clock)
: base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
try
{
Exception ex;
var key = Request.Headers[Options.HeaderName].First();
if (!IsValid(key, out ex))
{
return Task.FromResult(AuthenticateResult.Fail(ex.Message));
//filterContext.Result = new CustomUnauthorizedResult(ex.Message);
}
else
{
AuthenticationTicket ticket = new AuthenticationTicket(new ClaimsPrincipal(),new AuthenticationProperties(),this.Scheme.Name);
return Task.FromResult(AuthenticateResult.Success(ticket));
}
}
catch (InvalidOperationException)
{
return Task.FromResult(AuthenticateResult.Fail(""));
}
}
}
Run Code Online (Sandbox Code Playgroud)
自定义身份验证扩展
public static …Run Code Online (Sandbox Code Playgroud) 我正在使用 web api 来访问数据,我想对 web api 进行身份验证和授权。为此,我正在使用 JWT 令牌身份验证。但我不知道应该在哪里存储访问令牌?
我想做的事?
1)登录后存储令牌
2)如果用户想访问任何web api方法,请检查该用户的令牌是否有效,如果有效则授予访问权限。
我知道两种方式
1)使用cookies
2)sql server 数据库
哪一种是从上面存储令牌的更好方法?
我想在材料 Input 中使用 ngb-datepicker 。我成功地将 datepicker 应用于 mat 输入。但是月份和年份下拉列表存在问题。
我尝试了以下代码段:
<mat-form-field>
<input matInput placeholder="Select Valid Till" name="GodownValidTill" [(ngModel)]="GodownValidTill" ngbDatepicker #d2="ngbDatepicker" required readonly>
<mat-icon style="float:right;height:20px;width:20px;" (click)="d2.toggle()" svgIcon="calendar" title="Calendar"></mat-icon>
</mat-form-field>
Run Code Online (Sandbox Code Playgroud)
有没有完美工作的解决方案?
我正在从我的客户端项目调用 web api 方法并将值从响应头返回给客户端
[HttpPost]
[AllowAnonymous]
[Route("login")]
public IActionResult Login(Users user)
{
string s = _repository.Users.ValidateUser(user);
if (s == "success")
{
Users u = _repository.Users.FindByUserName(user.UserName);
int pid = u.PersonId;
string role = "";
if (u != null)
{
role = _repository.RoleManager.GetRole(u.Id);
}
Response.Headers.Add("Role", role);
return Ok(pid.ToString());
}
}
Run Code Online (Sandbox Code Playgroud)
我在下面尝试在客户端应用程序中获取标头值
[HttpPost]
public IActionResult Login(Users user)
{
string pid = "";
HttpClient client = service.GetService();
HttpResponseMessage response = client.PostAsJsonAsync("api/mymethod", user).Result;
Task<string> tokenResult = response.Content.ReadAsAsync<string>();
pid = tokenResult.Result.ToString();
var headers = response.Headers;
string …Run Code Online (Sandbox Code Playgroud)