{"id":4658,"date":"2021-02-22T14:58:37","date_gmt":"2021-02-22T13:58:37","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=4658"},"modified":"2022-07-25T08:31:51","modified_gmt":"2022-07-25T07:31:51","slug":"c-webapi-custom-authentication","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-webapi-custom-authentication\/","title":{"rendered":"C# WebAPI Custom Authentication"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public void ConfigureServices(IServiceCollection services)\n{\n\tservices.AddAuthentication(CustomAuthHandler.SchemeName)\n\t\t.AddCustomAuth(options => { });\n}\n\npublic class CustomAuthHandler : AuthenticationHandler&lt;CustomAuthOptions>\n{\n\tpublic static readonly string SchemeName = \"custom\";\n\tpublic CustomAuthHandler(IOptionsMonitor&lt;CustomAuthOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)\n\t{\n\n\t}\n\n\tprotected override Task&lt;AuthenticateResult> HandleAuthenticateAsync()\n\t{\n\t\t\/\/return AuthenticateResult.NoResult();\n\t\t\/\/return AuthenticateResult.Fail(new System.Exception(\"Expected bearer token\"));\n\t\tif (Context.User.Identity.IsAuthenticated)\n\t\t\treturn Task.FromResult(AuthenticateResult.NoResult());\n\n\t\ttry\n\t\t{\n\t\t\tvar claims = new List&lt;Claim> {\n\t\t\t\tnew Claim(ClaimTypes.NameIdentifier, \"test@test.lan\"),\n\t\t\t\tnew Claim(ClaimTypes.Name, \"test@test.lan\"),\n\t\t\t};\n\t\t\tvar claimsIdentity = new ClaimsIdentity(claims, CustomAuthHandler.SchemeName, \"userId\", \"roleId\");\n\t\t\tvar principal = new ClaimsPrincipal(claimsIdentity);\n\t\t\tvar properties = new AuthenticationProperties();\n\t\t\treturn Task.FromResult(AuthenticateResult.Success(new AuthenticationTicket(principal, properties, CustomAuthHandler.SchemeName)));\n\t\t}\n\t\tcatch (Exception ex)\n\t\t{\n\t\t\treturn Task.FromResult(AuthenticateResult.Fail(ex.Message));\n\t\t}\n\t}\n}\npublic class CustomAuthOptions : AuthenticationSchemeOptions\n{\n\n}\n\npublic static class CustomAuthExtensions\n{\n\tpublic static AuthenticationBuilder AddCustomAuth(this AuthenticationBuilder builder, Action&lt;CustomAuthOptions> configureOptions)\n\t{\n\t\treturn builder.AddScheme&lt;CustomAuthOptions, CustomAuthHandler>(CustomAuthHandler.SchemeName, CustomAuthHandler.SchemeName, configureOptions);\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-4658","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4658","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=4658"}],"version-history":[{"count":1,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4658\/revisions"}],"predecessor-version":[{"id":4659,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/4658\/revisions\/4659"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=4658"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=4658"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=4658"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}