Son Haberler
Anasayfa / Asp.NET MVC / SingalR Login (header authorized)

SingalR Login (header authorized)

SingalR teknolojisini kullanarak kendimize authorized oluşturalım ve sorgu yaparkende bağlantı güvenliğini sağlayalım.

ilk önce authorized oluşturalım. Arkasındanda hub oluşturarak SingalR servisimi yapalım.

 

using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Web;
using Microsoft.AspNet.SignalR.Hubs;
using System.Data.SqlClient;
using System.Data;

namespace TESTAPP
{
    public class AuthorizeClaimsAttribute : AuthorizeAttribute
    {
        public static Users AutUser = new Users();
        public bool? _Allow;
        public bool Allow { get { return (_Allow == null) ? false : Convert.ToBoolean(_Allow); } set { _Allow = value; } }
        public override bool AuthorizeHubConnection(HubDescriptor hubDescriptor, IRequest request)
        {
            return true;
        }
        public override bool AuthorizeHubMethodInvocation(IHubIncomingInvokerContext hubIncomingInvokerContext, bool appliesToMethod)
        {
            if (Allow)
                return true;
            else
            {
                var token = hubIncomingInvokerContext.Hub.Context.Headers;
                AutUser.USER = token["user"];
        AutUser.PASS = token["pass"];
                return AutUser.IsValid();
            }
        }

        protected override bool UserAuthorized(System.Security.Principal.IPrincipal user)
        {
            return true;
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }

            var principal = user as ClaimsPrincipal;

            if (principal != null)
            {
                Claim authenticated = principal.FindFirst(ClaimTypes.Authentication);
                if (authenticated != null && authenticated.Value == "true")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            else
            {
                return false;
            }
        }
    }
    public class Users
    {
        private DesmerMOBIL.DAL.Models.DesmerMOBILEntities db = new DAL.Models.DesmerMOBILEntities();
        public string USER { get; set; }
    public string PASS { get; set; }
        public string RESULT { get; set; }
        public int KullaniciID;
        public bool Login(string user, string pass)
        {
            
            if (USER == PASS)
            {
                RESULT = "sonuc";
                return true;
            }
            else
                return false;
        }
        public bool IsValid()
        {
            var result = Login(this.USER, this.PASS);
            switch (result)
            {
                case true:
                    return true;
                case false:
                    return false;
                default:
                    return false;
            }
        }
    }
}

ve SingalR hubmız aşağıda

using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;
using Microsoft.AspNet.SignalR.Owin;
using System.Data.SqlClient;
using System.Data;

namespace TESTAPP
{
    public class SingalRServer : Hub
    {
        public class UserLogin
        {
            public string USER { get; set; }
      public string PASS { get; set; }
            public string RESULT { get; set; }
        }

        [AuthorizeClaims(Allow = true)]
        public UserLogin SendLogin(UserLogin user)
        {
            Clients.All.LoginReceived(AuthorizeClaimsAttribute.AutUser.USER,AuthorizeClaimsAttribute.AutUser.PASS);
            AuthorizeClaimsAttribute.AutUser.USER = user.USER;
            if (AuthorizeClaimsAttribute.AutUser.IsValid())
            {
                user.RESULT = AuthorizeClaimsAttribute.AutUser.RESULT;
                return user;
            }
            else
            {
                UserLogin user2 = new UserLogin { RESULT = "" };
                return user2;
            }


        }
        [AuthorizeClaims(Allow = false)]
        public string Hello()
        {
            return "sonuc";
        }
    }

}