C
C#7mo ago
lux

✅ oauth in asp net framework?

hi all, i have created a redirect url for oauth clients how can i redirect my deployed asp net project in iis to redirect to do the authentication? this is then authentication controller public class AuthController : Controller { public ActionResult Index() { return View(); } [Route("auth/redirect")] [HttpGet] public ActionResult Redirect() { string redirectScript = "<html><script>window.location.href=window.location.href.Replace('#', '?').Replace('redirect', 'AuthRedirect')</script></html>"; return Content(redirectScript, "text/html"); } [HttpGet] public ActionResult AuthRedirect(string access_token) { // Get the full URL from the request var url = Request.Url.ToString(); // Extract the fragment identifier var fragment = url.Substring(url.IndexOf('#') + 1); // Parse the fragment to extract the access_token parameter value var queryString = HttpUtility.ParseQueryString(fragment); var accessToken = queryString["access_token"]; var cookie = new HttpCookie("otsession", accessToken) { Path = "/", HttpOnly = true, }; Response.Cookies.Add(cookie); return RedirectToAction("Index", "Home"); } } this is the oauth link http://localhost:8080/otdsws/login?response_type=token&client_id=<oauth client ID>&redirect_uri=<successful authentication redirect url>&state=none i have never created an implementation of oauth before.. so any assistance will be appreciated
17 Replies
JakenVeina
JakenVeina7mo ago
are you asking how to do a redirect, or how OAuth works?
lux
lux7mo ago
i think need to do redirect for the oauth to works? i actually havent implemented oauth before.. so i thought it ishould redirect first? like when i access my web index homepage then we will redirect user and authenticate that user using oauth so determine if they're allowed to access the homepage..
JakenVeina
JakenVeina7mo ago
this is an MVC app? or RazorPages? or something else?
lux
lux7mo ago
yes asp net framework with mvc..
JakenVeina
JakenVeina7mo ago
who is your OAuth provider?
lux
lux7mo ago
so i created the redirect url already using oauth clients..
No description
lux
lux7mo ago
its an opentext content server.. its deployed internally so i access my webpage from this link http://192.168.1.86/otcsnas
JakenVeina
JakenVeina7mo ago
so, a separate app of your own?
lux
lux7mo ago
yes.. a separate one.. the http://192.168.1.86/otcsnas is asp net framework mvc project deployed with iis.. when i access http://localhost:8080/otdsws/login?response_type=token&client_id=otcsnas&redirect_uri=https://localhost:7040/redirect&state=none im able to get a token from link..
JakenVeina
JakenVeina7mo ago
otcsnas is your main app and otdsws is the OAuth provider?
lux
lux7mo ago
yes otcsnas is my main app and otdsws is the OAuth provider..
JakenVeina
JakenVeina7mo ago
alrighty, so the first step is to have your main app redirect to the provider for login which reveals the first problem your main app has to know what URL to redirect to that could range from you just hard-coding it, to retrieving it automatically from .well-known/openid-configuration on the provider domain up to you how flexible you want to be so, you'll setup a "Login" link or whatever in your main app which either A) calls a controller method, which calculates a login URL and issues a redirect for it or B) just already has the calculated login URL in it
lux
lux7mo ago
redirect to provider, meaning when user access homepage (in this case is HomeController) to redirect to the OAuth link ? http://localhost:8080/otdsws/login?response_type=token&client_id=otcsnas&redirect_uri=https://localhost:7040/redirect&state=none -> (the OAuth link that will return token in the link)
JakenVeina
JakenVeina7mo ago
yup that URL takes the user to the OAuth provider's login page, where they actually put in their credentials and tells the provider which app is asking for authentication, and what its needs are I'll assume that URL is correct, cause you're the only one who's gonna know what parameters are required or not depends on the provider except for client_id and redirect_uri those are kinda required assuming the user correctly enters their credentials, the OAuth provider then redirects them back to the redirect_uri you provided with the access token you asked for at this point, it's up to you and the provider what needs to happen with that token most of the time, the token is only temporary and doesn't actually do anything it has to be securely exchanged for a REAL token I.E. your server sends another request behind the scenes to the OAuth provider, with the token, and the client_secret to confirm that "yes, when you received a request with my client_id it really was me this flow prevents the client_secret from ever being shared beyond your server and the OAuth provider so, whether you run an exchange to get it or not, once you have a proper access token, you need to use it for...whatever you need it for in this case, if all you're trying to do is login the user, you probably need to use the access token to make another behind-the-scenes call to the OAuth provider to actually GET the user's info maybe something like GET ~/users/@me or GET ~/users/current or whatever alternatively, maybe the token you retrieved is a JWT that has that info encoded directly in it now, you know who the user is, and you can establish a "session" for that user within your own app, and don't need the OAuth provider anymore maybe you hang on to that access token, maybe you throw it away, maybe you actually instruct the OAuth provider to revoke it, to prevent it from being leaked your /redirect controller method within your app does all this, and then finally issues a redirect to the user, to "Index" or whatever
lux
lux7mo ago
thank you so much for your explanation 🙏 🙏 im still trying to understand your explanation and explore more... thanks so much ... hope i can get back to you on some questions if i have any doubt on this if is okay 🙏 ..
JakenVeina
JakenVeina7mo ago
absolutely bottom line, you've got 2 things to implement in your main app A) you need to be able to calculate the URL to send users to the OAuth provider B) you need to write your redirect method that the OAuth sends the user back to
lux
lux7mo ago
okay can 🙏 i will try to implement it first and update you the results.. hi @V.EINA Jaken , i wanted to update you im able to get the access_token from the link now.. and i only need to validate the user in asp net project now.. i wanted to thank you for your detailed explanation on OAuth , if not i dont even understand how to implement it in my asp net project, ive tried to search for tutorials but it implementation was complicated without first understanding the concept.. thanks so much for your help!! 🙏 @V.EINA Jaken
Want results from more Discord servers?
Add your server
More Posts
❔ Best, most reliable NER (Named Entity Recognition) Library for .NET?I'm looking for a NER library that is supported with .net framework & .net core. I do not want to ❔ Organized Function / method libraries?In C# i want to try making some reusable methods/functions. In Unreal Engine, there is a "Function L✅ How do you control which WPF property is the default for that class?Examine the following XAML: ```XAML <SomeClass> PropertyValue </SomeClass> <OtherClass> <OtherCl❔ ✅ WPF Class derivation where both classes have XAML pagesIs it OK for base WPF class to have a XAML page and its derived class to also declare one? So we'd ❔ Adding up checkBoxes and Radio Buttons into list boxhttps://gyazo.com/797f6e3e82d121c43a5895520421ebb3 This is my form looking for some help getting my ❔ Hostbuilder, WPF and adding more class library'sI have my WPF project where i use hostbuilder to handle some dependency's, this works good with anot❔ Why isn't one of my Identity user field being updated?Code: https://gist.github.com/JsPeanut/b59cc1d4da3ababc532fe3b6e8a9ad69 The void `ChangeUserCategor❔ 0x80070422 error when starting UWP projectHello!!!!!!!! So when I try to start an UWP project in Visual Studio, it does not work and the follo❔ TicTacToe Minimax isnt working properlyHello ive been trying to make a tictactoe game where the player plays against an AI that uses the mi✅ Styling Expander Avalonia```xml <Style Selector="Expander"> <!-- changes styles of content inside drop down --> <