2012-11-23 45 views
1

我有一个基本的WPF应用程序,客户端写入数据库。我在服务器2012计算机上使用IIS来托管Web服务。我正在尝试实现Forms身份验证,并且我已经完成了所有工作(从客户端传递用户名和密码到xaml.cs,验证我的ASP.NET用户是否有效)。然后,我想实现ASP.NET角色授权我们应该使用的方法是“[PrincipalPermission(SecurityAction.Demand,Role =”Allowed“)]”WCF Forms使用ASP.NET角色的身份验证授权=访问被拒绝?

理论上这应该只是使用传递的凭据在客户端(我已经确认)工作时,当我尝试点击按钮时,它应该检查我传递的用户是否在角色中,如果是,则允许,否则拒绝。但是,用户是在它仍然说“访问被拒绝”的角色。

任何想法?

using System; 
using System.Collections.Generic; 
using System.Data.Entity.Validation; 
using System.Diagnostics; 
using System.Linq; 
using System.ServiceModel; 
using System.Security.Permissions; 
using RequestRepository; 
using System.Threading; 
using System.Web; 

namespace RequestServiceLibrary 
{ 
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 
    public class RequestService : IRequestService 
    { 
    private List<Request> requests = new List<Request>(); 
    private RequestLibraryEntities context = new RequestLibraryEntities(); 

    [PrincipalPermission(SecurityAction.Demand, Role = "Allowed")] 
    public string SubmitRequest(Request req) 
    { 
     Thread.CurrentPrincipal = HttpContext.Current.User; 
     if (context.Requests.Count() == 0) 
      populateRequests(); 
     req.Id = Guid.NewGuid().ToString(); 
     req.TimeSubmitted = DateTime.Now; 
     requests.Add(req); 
     addRequest(req); 
     return req.Id; 
    } 

    [PrincipalPermission(SecurityAction.Demand, Role = "Allowed")] 
    public bool UpdateRequest(Request req) 
    { 
     Thread.CurrentPrincipal = HttpContext.Current.User; 
     bool returnval = false; 
     try 
     { 
      var getobject = requests.Find(x => x.Id.Equals(req.Id)); 
      if (getobject != null) //checks to make sure the object isn't empty 
      { 
       getobject.Username = req.Username; 
       getobject.Password = req.Password; 
       getobject.RequestedResource = req.RequestedResource; 
       getobject.TimeSubmitted = req.TimeSubmitted; 
      } 
      //Find the request object in the database 
      var Id = Guid.Parse(req.Id); 
      var rl = context.Requests.Find(Id); 
      //Update that object with the values from req    
      rl.Username = req.Username; 
      rl.Password = req.Password; 
      rl.RequestedResource = req.RequestedResource; 
      rl.TimeTransmitted = req.TimeSubmitted; 
      context.SaveChanges(); 
      returnval = true; 
      return returnval; 
     } 
     catch (Exception) { return returnval; } 
    } 
    public List<Request> GetRequests() 
    { 
     populateRequests(); 
     return requests; 
    } 

    [PrincipalPermission(SecurityAction.Demand, Role = "Disallowed")] 
    public bool RemoveRequest(string id) 
    { 
     bool rval = false; 
     try 
     { 
      Request req = requests.Find(x => x.Id.Equals(id)); 
      requests.Remove(req); 
      rval = delRequest(req); 
      return rval; 
     } 
     catch (Exception) 
     { 
      return false; 
     } 
    } 


    private void populateRequests() 
    { 
     requests = new List<Request>(); 
     var rl = context.Requests.ToList(); 
     foreach (var r in rl) 
     { 
      requests.Add(new Request() 
      { 
       Id = r.Id.ToString(), 
       Password = r.Password, 
       RequestedResource = r.RequestedResource, 
       TimeSubmitted = r.TimeTransmitted, 
       Username = r.Username 
      }); 
     } 
    } 

    private void addRequest(Request req) 
    { 
     try 
     { 
      var r = context.Requests.Create(); 
      r.Id = Guid.Parse(req.Id); 
      r.Username = req.Username; 
      r.Password = req.Password; 
      r.RequestedResource = req.RequestedResource; 
      r.TimeTransmitted = req.TimeSubmitted; 
      context.Requests.Add(r); 
      context.SaveChanges(); 
     } 
     catch (DbEntityValidationException dbEx) 
     { 
      foreach (var validationErrors in dbEx.EntityValidationErrors) 
      { 
       foreach (var validationError in validationErrors.ValidationErrors) 
       { 
        Console.WriteLine("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage); 
       } 
      } 
     } 
    } 


    private bool delRequest(Request req) 
    { 
     Guid Id = Guid.Parse(req.Id); 
     var r = context.Requests.Create(); 
     r.Id = Id; 
     var rl = context.Requests.Find(Id); 
     try 
     { 
      context.Requests.Remove(rl); 
      context.SaveChanges(); 
      return true; 
     } 
     catch (Exception) { return false; } 
    } 

    } 
} 

回答

0

为了能够以这种方式使用PrincipalPermissionAttribute,您需要先设置Thread.CurrentPrincipal与适当的角色(“允许”,在这种情况下)的负责人。

例如,您可以使用ClientRoleProvider来执行此操作,或者只需手动创建委托人(可能使用从Web服务检索到的角色)。