开发者

Silverlight Ria Authentication - Checking Authentication Before Using DomainContext

开发者 https://www.devze.com 2023-02-14 18:19 出处:网络
I have the beginnings of a standard Silverlight/ RIA/ EF application that is just working straight away as expected.

I have the beginnings of a standard Silverlight/ RIA/ EF application that is just working straight away as expected.

I wanted to restrict my entire DomainService to only authenticated users, as the application will eventually live on the public internet, and all data access needs to be user authenticated.

The problem I have, is that I cannot use the auto-generated DomainContext class in my Silverlight app unless I wrap all of its Load methods inside a custom class that verifies the authentication status of the user before attempting to retrieve data - which seems like a long and tedious coding task.

Surely there must be a simple solutio开发者_如何转开发n that Ive missed ?

This stuff was easy in ASP.NET because the moment you lost (or never had) authentication you were redirected to a login page (as configured in web.config).

Im really suprised theres no similar mechanism in Silverlight, as it seems to be to be a standard business application requirement.


Requiring authentication in your DomainService is easy. Just add a [RequiresAuthentication] or [RequiresRole] attribute to either the class (applies to all operations) or operation you want to authorize. You might find these links helpful.

How to: Enable Authentication in RIA Services

RequiresAuthenticationAttribute


I am not 100% sire if this is the answer you want and if it's a good practice to do it like this but you could implement a message inspector that inspects whether the user is authenticated like this:

public class ClientCustomHeadersDispatchMessageInspector : IDispatchMessageInspector
{

    public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
    {
        if (!HttpContext.Current.User.Identity.IsAuthenticated) {
            throw new SecurityException("User not authenticated");
        }
        return null;
    }


    public void BeforeSendReply(ref Message reply, object correlationState)
    {
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号