/// <summary>
        /// Foydalanuvchini to`kenini olish
        /// </summary>
        /// <param name="user">Foydalanuvchini ma`lumotlari</param>
        /// <returns></returns>
        private string GetToken(ApiUser user)
        {
            var token = new JwtSecurityToken(
                AuthConstants.ValidIssuer,
                AuthConstants.ValidAudience,
                new[] { new Claim(nameof(ApiUser.Id), user.Id.ToString()) },
                expires: DateTime.Now.AddMinutes(5),
                signingCredentials: new SigningCredentials(
                    AuthConstants.GetSecurityKey(),
                    SecurityAlgorithms.HmacSha256Signature
                    )
                );

            return(new JwtSecurityTokenHandler().WriteToken(token));
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Apidagi servislarni sozlash
        /// </summary>
        /// <param name="services"></param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            var parametres = new TokenValidationParameters()
            {
                ValidIssuer    = AuthConstants.ValidIssuer,
                ValidAudience  = AuthConstants.ValidAudience,
                ValidateIssuer = true,

                IssuerSigningKey = AuthConstants.GetSecurityKey(),
                ValidateLifetime = true
            };

            services.AddAuthentication(options => options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme)
            .AddJwtBearer(o => o.TokenValidationParameters = parametres);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Request for Token
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        private string Token(User model)
        {
            string token           = string.Empty;
            var    request         = HttpContext.Current.Request;
            var    tokenServiceUrl = request.Url.GetLeftPart(UriPartial.Authority) + request.ApplicationPath + "token";

            using (WebClient client = new WebClient())
            {
                client.Headers.Add(RequestConstants.CONTENT_TYPE, RequestConstants.FORM_TYPE);
                token = client.UploadString(tokenServiceUrl, RequestConstants.POST, AuthConstants.GenerateRequest(model.Email, model.Password));
            }
            Console.Write(token);
            JObject json = JObject.Parse(token);

            token = json[AuthConstants.ACCESS_TOKEN].ToString();
            return(token);
        }
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            // this is a common way for refreshing Application Bar buttons
            this._addNoteAppButton     = (ApplicationBarIconButton)ApplicationBar.Buttons[0];
            this._selectAppButton      = (ApplicationBarIconButton)ApplicationBar.Buttons[1];
            this._removeNotesAppButton = (ApplicationBarIconButton)ApplicationBar.Buttons[2];

            this.DataContext = App.ViewModel;

            // requesting OpenID Connect app creds from server.
            var client = new WebClient();

            client.OpenReadCompleted += (_, args) =>
            {
                if ((args.Error != null) || (args.Result == null))
                {
                    MessageBox.Show("Failed to get OpenID Connect app creds from server. Please, create OauthAppCredentials.json file in server's root and fill it with your own values!");
                    return;
                }

                var authConstants = AuthConstants.FromJsonStream(args.Result);
                App.ViewModel.AuthSchema = authConstants.JwtAuthSchema;

                // creating the SignInButton dynamically, because it doesn't support delayed ClientId initialization
                var signinButton = new SignInButton()
                {
                    TextType = ButtonTextType.SignIn,
                    Branding = BrandingType.MicrosoftAccount,
                    Scopes   = "wl.signin wl.basic",
                    ClientId = authConstants.LiveClientId
                };
                Grid.SetRow(signinButton, 1);
                signinButton.SessionChanged += this.SignInButton_OnSessionChanged;

                this.LayoutRoot.Children.Add(signinButton);
            };

            client.OpenReadAsync(new Uri(MainViewModel.ServiceUri + "/AuthConstants.json"));
        }