asp.net mvc auth2.0简化版——客户端
- public partial class Login : Page
- {
- public string client_id = "123456789";
- public string getCodeUrl = "http://localhost:8080/?client_id={0}>response_type={1}>redirect_uri={2}>scope={3}>state={4}";
- public string getTokenUrl = "http://localhost:8080/Auth/Token?code={0}>grant_type={1}>client_id={2}";
- public string getUserUrl = "http://localhost:8080/Auth/UserInfo?token={0}";
- protected void Page_Load(object sender, EventArgs e)
- {
- string code = Request.QueryString["code"] ?? "";
- if (string.IsNullOrEmpty(code))
- {
- //第一步,请求获取code(请求OAuth服务器)
- string response_type = "code";
- string redirect_uri = HttpUtility.UrlEncode("http://localhost:9999/Account/Login.aspx");
- string scope = "";
- string state = "";
- string url = string.Format(getCodeUrl, client_id, response_type, redirect_uri, scope, state);
- Response.Redirect(url);
- }
- else
- {
- //第二步,获取code之后请求获取token(请求OAuth服务器)
- WebClient web = new WebClient();
- web.Encoding = Encoding.UTF8;
- var url = string.Format(getTokenUrl, code, "authorization_code", client_id);
- var token = web.DownloadString(url).Replace("\"","");;
- //第三部,获取token之后,获取user信息(请求OAuth服务器)
- url = string.Format(getUserUrl, token);
- var userinfo = web.DownloadString(url);
- //把CODE保存下来
- var cookie = new HttpCookie("token");
- cookie.Value = HttpUtility.UrlEncode(token);
- cookie.Expires = DateTime.Now.AddHours(1);
- Response.Cookies.Add(cookie);
- var cookie1 = new HttpCookie("code");
- cookie1.Value = HttpUtility.UrlEncode(code);
- cookie1.Expires = DateTime.Now.AddHours(1);
- Response.Cookies.Add(cookie1);
- //返回获取到的用户信息
- //return this.Json("userInfoContent=" + userInfoContent, JsonRequestBehavior.AllowGet);
- var cookie2 = new HttpCookie("user");
- cookie2.Value = HttpUtility.UrlEncode(userinfo);
- cookie2.Expires = DateTime.Now.AddHours(1);
- Response.Cookies.Add(cookie2);
- Response.Redirect("/");
- }
- }
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · asp.net mvc auth2.0简化版——服务端
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification