2012-06-11 22 views
2

我試圖在我的MVC應用程序中提供iCalendar文件(.ics)。在ASPNET MVC中使用身份驗證服務iCalendar文件

到目前爲止它工作正常。我有一個iPhone訂閱日曆的URL,但現在我需要爲每個用戶提供個性化日曆。

訂閱iPhone上的日曆時,我可以輸入用戶名和密碼,但我不知道如何在我的MVC應用程序中訪問這些日曆。

我在哪裏可以找到身份驗證如何工作以及如何實現的詳細信息?

回答

2

事實證明,基本認證是必需的。我有一半工作,但我的IIS配置阻礙了。所以,當沒有授權標題時,僅僅返回401響應會導致客戶端(例如iPhone)需要用戶名/密碼來訂閱日曆。

在有授權請求頭的請求的授權中,可以處理基本認證,從base 64編碼的字符串中檢索用戶名和密碼。

下面是MVC一些有用的代碼:

public class BasicAuthorizeAttribute : AuthorizeAttribute 
{ 
    public override void OnAuthorization(AuthorizationContext filterContext) 
    { 
     if (filterContext == null) 
     { 
      throw new ArgumentNullException("filterContext"); 
     } 

     var auth = filterContext.HttpContext.Request.Headers["Authorization"]; 

     if (!String.IsNullOrEmpty(auth)) 
     { 
      var encodedDataAsBytes = Convert.FromBase64String(auth.Replace("Basic ", "")); 
      var value = Encoding.ASCII.GetString(encodedDataAsBytes); 
      var username = value.Substring(0, value.IndexOf(':')); 
      var password = value.Substring(value.IndexOf(':') + 1); 

      if (MembershipService.ValidateUser(username, password)) 
      { 
       filterContext.HttpContext.User = new GenericPrincipal(new GenericIdentity(username), null); 
      } 
      else 
      { 
       filterContext.Result = new HttpStatusCodeResult(401); 
      } 
     } 
     else 
     { 
      if (AuthorizeCore(filterContext.HttpContext)) 
      { 
       var cachePolicy = filterContext.HttpContext.Response.Cache; 
       cachePolicy.SetProxyMaxAge(new TimeSpan(0)); 
       cachePolicy.AddValidationCallback(CacheValidateHandler, null); 
      } 
      else 
      { 
       filterContext.HttpContext.Response.Clear(); 
       filterContext.HttpContext.Response.StatusDescription = "Unauthorized"; 
       filterContext.HttpContext.Response.AddHeader("WWW-Authenticate", "Basic realm=\"Secure Calendar\""); 
       filterContext.HttpContext.Response.Write("401, please authenticate"); 
       filterContext.HttpContext.Response.StatusCode = 401; 
       filterContext.Result = new EmptyResult(); 
       filterContext.HttpContext.Response.End(); 
      } 
     } 
    } 

    private void CacheValidateHandler(HttpContext context, object data, ref HttpValidationStatus validationStatus) 
    { 
     validationStatus = OnCacheAuthorization(new HttpContextWrapper(context)); 
    } 
} 

然後,我的控制器操作是這樣的:

[BasicAuthorize] 
public ActionResult Calendar() 
{ 
    var userName = HttpContext.User.Identity.Name; 
    var appointments = GetAppointments(userName); 
    return new CalendarResult(appointments, "Appointments.ics"); 
} 
0

我發現這真的幫助很大,但我在開發過程中打了幾個問題和我想我會分享他們中的一些來幫助拯救其他人一段時間。

我正在尋找從我的web應用程序中獲取數據到Android設備的日曆中,並且我使用的是discountasp作爲託管服務。

我碰到的第一個問題是驗證在上傳到服務器時無法正常工作,足夠接受我的控制面板登錄爲discountasp而不是我的表單登錄。

答案是關閉IIS管理器中的基本身份驗證。這解決了這個問題。

其次,我用來將日曆同步到android設備的應用程序被稱爲iCalSync2 - 它是一個不錯的應用程序,運行良好。但我發現只有當文件以.ics格式(因爲某些原因,我把它作爲一個.ical ..它一定是遲了)交付文件才能正常工作,而且我還必須選擇webcal選項

最後,我發現我必須添加webcal://,而不是http://

也要小心,因爲上面的代碼忽略了輸入變量的角色,並且始終沒有通過任何內容,因此您可能需要執行日曆例程中的一些基於角色的檢查或修改上面的代碼以處理角色變量。