public static IApplicationBuilder UseSampleauthSdk(this IApplicationBuilder app, Action <SampleauthOptions> setupAction = null) { if (setupAction == null) { app.UseMiddleware <SampleauthMiddleware>(); } else { SampleauthOptions authOptions = new SampleauthOptions(); setupAction(authOptions); UseMiddlewareExtensions.UseMiddleware <SampleauthMiddleware>(app, new object[1] { authOptions }); } return(app); }
public async Task Invoke(HttpContext context) { _options = _options ?? context.RequestServices.GetService <IOptions <SampleauthOptions> >()?.Value; var _sampleauthList = _options?.SampleauthList; if (_sampleauthList?.Count == 0) { Console.WriteLine("SampleauthSdk no config users!!!"); //未配置用户则不启用 await _next(context); return; } var _method = context.Request.Method.ToLower(); var _path = context.Request.PathBase.Value + context.Request.Path.Value; var _routePrefix = _options.RoutePrefix ?? string.Empty; var _loginPath = _options.LoginPath; var _logoutPath = _options.LogoutPath; if (_options.PathIsConvertToLower) { _routePrefix = _routePrefix.ToLower(); _path = _path.ToLower(); _loginPath = _loginPath.ToLower(); _logoutPath = _logoutPath.ToLower(); } if (_routePrefix.Contains(",") || _routePrefix.Contains("|")) { var morePrefixs = _routePrefix.Split(new char[] { ',', '|' }, StringSplitOptions.RemoveEmptyEntries).Select(s => "/" + s); //为空 _routePrefix = morePrefixs.FirstOrDefault(s => (_path + "/").StartsWith(s + "/")); } if (!string.IsNullOrEmpty(_routePrefix) && !_routePrefix.StartsWith("/")) { _routePrefix = "/" + _routePrefix; } if (_routePrefix == null || !(_path + "/").StartsWith(_routePrefix + "/")) { await _next(context); return; } else if (_path.StartsWith($"{_routePrefix}/{_loginPath}")) { //登录 if (_method == "get") { await WriteStaticPage(context, LOGIN_FILE_NAME, _options.GetPageConfig()); return; } else if (_method == "post") { string username = context.Request.Form["username"].ToString().Trim(); string userpwd = context.Request.Form["userpwd"].ToString().Trim(); // 用户登录处理 if (_options.SignInBeforeHook(context, username, userpwd)) { return; } var existUser = _sampleauthList?.FirstOrDefault(s => s.Username == username && s.Userpwd == userpwd); // 用户是否存在 if (existUser == null) { context.Response.Redirect(GetRedirectPath(context, new Dictionary <string, string>() { { "msg", _options.GetPageConfig()[SampleauthPageConst.LoginTextErrorMsg] } })); return; } string userkey = existUser.Userkey; context.Response.Cookies.Append(SAMPLE_ATUH_SDK_COOKIE, Utils.SecurityHelper.GetSignToken(username, userkey)); string returnUrl = context.Request.Query["returnUrl"]; returnUrl = string.IsNullOrEmpty(returnUrl) ? $"{_routePrefix}/" : returnUrl; context.Response.Redirect(returnUrl); return; } } else if (_path.StartsWith($"{_routePrefix}/{_logoutPath}")) { // 用户校验处理 if (_options.SignOutBeforeHook(context)) { return; } //退出 context.Response.Cookies.Delete(SAMPLE_ATUH_SDK_COOKIE); context.Response.Redirect($"{_routePrefix}/{_loginPath}"); return; } else { // 用户校验处理 if (_options.SignCheckBeforeHook(context)) { await _next(context); return; } //身份验证 var encryptStr = context.Request.Cookies[SAMPLE_ATUH_SDK_COOKIE]; if (!string.IsNullOrEmpty(encryptStr) && _sampleauthList?.Any(s => SecurityHelper.GetSignToken(s.Username, s.Userkey) == encryptStr) == true) { await _next(context); return; } if (_options.DisabledAutoRedirectLogin) { context.Response.StatusCode = StatusCodes.Status401Unauthorized; return; } context.Response.Redirect($"{_routePrefix}/{_loginPath}?returnUrl=" + System.Web.HttpUtility.UrlEncode(GetRedirectPath(context))); return; } }
public SampleauthMiddleware(RequestDelegate next, SampleauthOptions options = null) { _next = next; _options = options; }