Last active
April 12, 2020 14:39
-
-
Save Neftedollar/f0c1ebf77f4590c58eaec8ab1015bd0f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// <PropertyGroup> | |
// <OutputType>Exe</OutputType> | |
// <TargetFramework>netcoreapp3.0</TargetFramework> | |
// </PropertyGroup> | |
type ApplicationUser(name, email) = | |
inherit MongoIdentityUser<Guid>(name, email) | |
new(name) = ApplicationUser(name,name) | |
new() = ApplicationUser() | |
let tokenHandler : HttpHandler = | |
fun (next: HttpFunc) (ctx:HttpContext) -> task { | |
let! login = ctx.BindJsonAsync<LoginForm>() | |
// let logger = ctx.GetService<ILogger>() | |
let userManager = ctx.GetService<UserManager<Identity.ApplicationUser>>() | |
Console.WriteLine("got userManager") | |
let user = userManager.Users.FirstOrDefault(fun u -> u.UserName = login.Login) | |
// let! user = userManager.FindByNameAsync(login.Login) | |
Console.WriteLine("got user") | |
Console.WriteLine(sprintf "user %O" user) | |
let! ok = userManager.CheckPasswordAsync(user, login.Password) | |
if not ok then | |
return! RequestErrors.BAD_REQUEST "User or password is not valid" next ctx | |
else | |
let! claims = userManager.GetClaimsAsync(user) | |
let token = Identity.generateToken JwtSettings claims | |
return! text token next ctx | |
} | |
let authorize = | |
requiresAuthentication (challenge JwtBearerDefaults.AuthenticationScheme) | |
let webApp = | |
choose [ | |
//... | |
POST >=> choose [ | |
//route "/register" >=> registerHandler | |
route "/token" >=> tokenHandler | |
] | |
route "/identiy" >=> authorize >=> text "its ok!" | |
] | |
//LOG: | |
// | |
//Microsoft.AspNetCore.Hosting.Diagnostics: Information: Request starting HTTP/1.1 POST http://localhost:8085/token text/plain 50 | |
//got userManager | |
//Stack overflow. | |
//Программа "[16513] Server.dll" завершилась с кодом 0 (0x0). | |
//если раскоментить 19 строку, и закоментить 18, то поведение абсолютно такое же | |
//let! user = userManager.FindByNameAsync(login.Login) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment