Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to fetch the username and password values during basic authentication when a json request is invoked in c#. I am using WCF service.

What I have tried:

I want to fetch the username and password values during basic authentication when a json request is invoked in c#. I am using WCF service.
Posted
Updated 15-Jan-23 22:08pm
v3

1 solution

We are not here to write your code for you. IF you are unsure where to start, then you need to start Googling for examples, tutorials, github repos, blog posts, YouTube videos, etc ... there is a lot of information out there.

1. OAuth 2.0: >oauth workflow - Google Search[^] found Which OAuth 2.0 Flow Should I Use?[^]
2. WCF: >wcf authentication - Google Search[^] found How to: Authenticate with a User Name and Password - WCF | Microsoft Learn[^]. Or, you can watch a video to see how it is done. Here are some YouTube videos: wcf authentication and authorization [^]

That shoud get you started. The WCF link[^] has examples.
 
Share this answer
 
v2
Comments
Member 7513082 18-Jan-23 4:17am    
I tried fetching the username and password from postman app as below:
HttpContext httpContext = HttpContext.Current;

string authHeader = httpContext.Request.Headers["Authorization"];

if (authHeader != null && authHeader.StartsWith("Basic"))
{
Common.WriteDDSLog("UserAuthentication()-Basic Authentication Started");
string encodedUsernamePassword = authHeader.Substring("Basic ".Length).Trim();
Encoding encoding = Encoding.GetEncoding("iso-8859-1");
string usernamePassword = encoding.GetString(Convert.FromBase64String(encodedUsernamePassword));

int seperatorIndex = usernamePassword.IndexOf(':');

var username = usernamePassword.Substring(0, seperatorIndex);
var password = usernamePassword.Substring(seperatorIndex + 1);
}
But getting object reference exception on debugging on reaching this code while invoking request
from postman
HttpContext httpContext = HttpContext.Current;

string authHeader = httpContext.Request.Headers["Authorization"];

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900