i need to config the oauth in my app manually, anyone have a tuto or documentation i can follow ?
authorization code flow
the client program.cs
<code>`builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "OAuth";
})
.AddCookie()
.AddOAuth("OAuth", options =>
{
options.ClientId = Environment.GetEnvironmentVariable("ClientId");
options.ClientSecret = Environment.GetEnvironmentVariable("ClientSecret");
options.CallbackPath = new PathString(Environment.GetEnvironmentVariable("CallbackPath"));
options.AuthorizationEndpoint = Environment.GetEnvironmentVariable("AuthorizationEndpoint");
options.TokenEndpoint = Environment.GetEnvironmentVariable("TokenEndpoint");
options.SaveTokens = true;
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
var accessToken = context.AccessToken;
var claims = new List<Claim>
{
new Claim("access_token", accessToken)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
context.Principal.AddIdentity(identity);
}
};
});`
</code>
<code>`builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "OAuth";
})
.AddCookie()
.AddOAuth("OAuth", options =>
{
options.ClientId = Environment.GetEnvironmentVariable("ClientId");
options.ClientSecret = Environment.GetEnvironmentVariable("ClientSecret");
options.CallbackPath = new PathString(Environment.GetEnvironmentVariable("CallbackPath"));
options.AuthorizationEndpoint = Environment.GetEnvironmentVariable("AuthorizationEndpoint");
options.TokenEndpoint = Environment.GetEnvironmentVariable("TokenEndpoint");
options.SaveTokens = true;
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
var accessToken = context.AccessToken;
var claims = new List<Claim>
{
new Claim("access_token", accessToken)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
context.Principal.AddIdentity(identity);
}
};
});`
</code>
`builder.Services.AddAuthentication(options =>
{
options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = "OAuth";
})
.AddCookie()
.AddOAuth("OAuth", options =>
{
options.ClientId = Environment.GetEnvironmentVariable("ClientId");
options.ClientSecret = Environment.GetEnvironmentVariable("ClientSecret");
options.CallbackPath = new PathString(Environment.GetEnvironmentVariable("CallbackPath"));
options.AuthorizationEndpoint = Environment.GetEnvironmentVariable("AuthorizationEndpoint");
options.TokenEndpoint = Environment.GetEnvironmentVariable("TokenEndpoint");
options.SaveTokens = true;
options.Events = new OAuthEvents
{
OnCreatingTicket = async context =>
{
var accessToken = context.AccessToken;
var claims = new List<Claim>
{
new Claim("access_token", accessToken)
};
var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
context.Principal.AddIdentity(identity);
}
};
});`
the auth program.cs
<code>`builder.Services.AddAuthentication("OAuth").AddJwtBearer("OAuth", options =>
{
var secretBytes = Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("Secret"));
var key = new SymmetricSecurityKey(secretBytes);
options.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.Zero,
ValidIssuer = Environment.GetEnvironmentVariable("Issuer"),
ValidAudience = Environment.GetEnvironmentVariable("Audience"),
IssuerSigningKey = key,
};
});`
</code>
<code>`builder.Services.AddAuthentication("OAuth").AddJwtBearer("OAuth", options =>
{
var secretBytes = Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("Secret"));
var key = new SymmetricSecurityKey(secretBytes);
options.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.Zero,
ValidIssuer = Environment.GetEnvironmentVariable("Issuer"),
ValidAudience = Environment.GetEnvironmentVariable("Audience"),
IssuerSigningKey = key,
};
});`
</code>
`builder.Services.AddAuthentication("OAuth").AddJwtBearer("OAuth", options =>
{
var secretBytes = Encoding.UTF8.GetBytes(Environment.GetEnvironmentVariable("Secret"));
var key = new SymmetricSecurityKey(secretBytes);
options.TokenValidationParameters = new TokenValidationParameters
{
ClockSkew = TimeSpan.Zero,
ValidIssuer = Environment.GetEnvironmentVariable("Issuer"),
ValidAudience = Environment.GetEnvironmentVariable("Audience"),
IssuerSigningKey = key,
};
});`
here the onpost for the login form
<code>
public IActionResult OnPost()
{
var redirectUri = Request.Form["redirect_uri"];
if (IsValidUser(Email, Password))
{
// Generate authorization code
var authorizationCode = Guid.NewGuid().ToString();
//AuthCodes[authorizationCode] = Email; // Store the code along with user identifier
// Construct redirect URI with authorization code and state
var query = new Dictionary<string, string>
{
{ "code", authorizationCode },
//{ "state", state }
};
var redirectUrl = QueryHelpers.AddQueryString(redirectUri, query);
// Redirect back to client application with authorization code
return Redirect(redirectUrl);
}
else
{
// Invalid credentials, handle accordingly (e.g., display error message)
return RedirectToAction("Login", new { error = "Invalid credentials" });
}
}
</code>
<code>
public IActionResult OnPost()
{
var redirectUri = Request.Form["redirect_uri"];
if (IsValidUser(Email, Password))
{
// Generate authorization code
var authorizationCode = Guid.NewGuid().ToString();
//AuthCodes[authorizationCode] = Email; // Store the code along with user identifier
// Construct redirect URI with authorization code and state
var query = new Dictionary<string, string>
{
{ "code", authorizationCode },
//{ "state", state }
};
var redirectUrl = QueryHelpers.AddQueryString(redirectUri, query);
// Redirect back to client application with authorization code
return Redirect(redirectUrl);
}
else
{
// Invalid credentials, handle accordingly (e.g., display error message)
return RedirectToAction("Login", new { error = "Invalid credentials" });
}
}
</code>
public IActionResult OnPost()
{
var redirectUri = Request.Form["redirect_uri"];
if (IsValidUser(Email, Password))
{
// Generate authorization code
var authorizationCode = Guid.NewGuid().ToString();
//AuthCodes[authorizationCode] = Email; // Store the code along with user identifier
// Construct redirect URI with authorization code and state
var query = new Dictionary<string, string>
{
{ "code", authorizationCode },
//{ "state", state }
};
var redirectUrl = QueryHelpers.AddQueryString(redirectUri, query);
// Redirect back to client application with authorization code
return Redirect(redirectUrl);
}
else
{
// Invalid credentials, handle accordingly (e.g., display error message)
return RedirectToAction("Login", new { error = "Invalid credentials" });
}
}
the problem i m facing that after the user succesuflly logs in , the auth server dsnt redirect to the client app , it redirects to the auth /account/callback
CallbackPath=/account/callback
New contributor
maatoug arken is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.