Example #1
0
 private (User, string) GetIdentity(PostParams param, ProviderType type)
 {
     var(user, ok, error) =
         type == ProviderType.Register ? identity.TryRegister(param.UserName, param.Password) :
         type == ProviderType.Login ? identity.TryLogin(param.UserName, param.Password) :
         type == ProviderType.Weibo ? identity.WeiboService.TryGetUserByWeibo(param.WeiboUid) :
         (null, false, "failed");
     if (ok)
     {
         return(user, null);
     }
     return(null, error);
 }
Example #2
0
        private async Task GenerateToken(HttpContext context, JsonBase model, ProviderType type)
        {
            var param = new PostParams();

            if (context.Request.ContentType != "application/json")
            {
                if (type == ProviderType.Weibo)
                {
                    param.WeiboUid = context.Request.Form["wuid"];
                }
                else
                {
                    param.UserName = context.Request.Form["user"];
                    param.Password = context.Request.Form["psd"];
                }
            }
            else
            {
                var user = JsonHelper.FromJson <JwtProviderParam>(await GetContentAsync(context));
                if (type == ProviderType.Weibo)
                {
                    param.WeiboUid = user.WeiboUID;
                }
                else
                {
                    param.UserName = user.UserName;
                    param.Password = user.Password;
                }
            }

            context.Response.ContentType = "application/json";

            var scopeFactory = serviceProvider.GetRequiredService <IServiceScopeFactory>();

            using (var scope = scopeFactory.CreateScope()) {
                this.identity = scope.ServiceProvider.GetRequiredService <IIdentityService <JwtSecurityToken, TokenProviderOptions, User> >();

                var(user, error) = GetIdentity(param, type);
                if (error != null)
                {
                    model.Message = error;
                    model.Status  = type == ProviderType.Register ? JsonStatus.RegisterFailed : JsonStatus.LoginFailed;
                    model.Content = new {
                        RedirectUrl = context.Request.Path
                    };
                    await context.Response.WriteAsync(JsonConvert.SerializeObject(model, new JsonSerializerSettings {
                        ContractResolver      = new LowercaseContractResolver(),
                        ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                        Formatting            = Formatting.Indented
                    }));

                    return;
                }

                model.Status  = JsonStatus.Succeed;
                model.Message = null;

                var vmProvider = new UserVM(user);
                if (type == ProviderType.Weibo)
                {
                    vmProvider = vmProvider.Create(this.identity.WeiboService.SearchWeiboUserInfoByUID);
                }

                model.Content = new TokenProviderVM {
                    Token   = new JwtSecurityTokenHandler().WriteToken(identity.GenerateToken(user, opts)),
                    Expires = (int)opts.Expiration.TotalSeconds,
                    Details = vmProvider.User
                };

                await context.Response.WriteAsync(JsonConvert.SerializeObject(model, new JsonSerializerSettings {
                    ContractResolver      = new LowercaseContractResolver(),
                    ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
                    Formatting            = Formatting.Indented
                }));
            }
        }